Class: Idb::SSHOperations

Inherits:
Object
  • Object
show all
Defined in:
lib/lib/ssh_operations.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, hostname, port) ⇒ SSHOperations

Returns a new instance of SSHOperations.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/lib/ssh_operations.rb', line 9

def initialize username, password, hostname, port
  @hostname = hostname
  @username = username
  @password = password
  @port = port

  $log.info "Establishing SSH Session for #{username}@#{hostname}:#{port}"

  begin
    @ssh = Net::SSH.start hostname, username, :password => password, :port => port

    # initiali:wze sftp connection and wait until it is open
    $log.info 'Establishing SFTP Session...'
    @sftp = Net::SFTP::Session.new @ssh
    @sftp.loop { @sftp.opening? }
  rescue Exception => ex
    $log.error ex.message
    error = Qt::MessageBox.new
    error.setInformativeText("SSH connection could not be established: #{ex.message}")
    error.setIcon(Qt::MessageBox::Critical)
    error.exec
  end
end

Instance Attribute Details

#sshObject

Returns the value of attribute ssh.



7
8
9
# File 'lib/lib/ssh_operations.rb', line 7

def ssh
  @ssh
end

Instance Method Details

#chmod(file, permissions) ⇒ Object



43
44
45
# File 'lib/lib/ssh_operations.rb', line 43

def chmod file, permissions
  @sftp.setstat(file, :permissions => permissions)
end

#dir_glob(path, pattern) ⇒ Object



98
99
100
# File 'lib/lib/ssh_operations.rb', line 98

def dir_glob path, pattern
  @sftp.dir.glob(path,pattern).map {|x| "#{path}/#{x.name}"}
end

#directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
109
110
111
112
# File 'lib/lib/ssh_operations.rb', line 106

def directory? path
  begin
    @sftp.stat!(path).directory?
  rescue
    nil
  end
end

#disconnectObject



33
34
35
36
# File 'lib/lib/ssh_operations.rb', line 33

def disconnect
  puts "[*] Closing SSH Session"
  @ssh.close
end

#download(remote_path, local_path = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/lib/ssh_operations.rb', line 56

def download(remote_path, local_path = nil)
  begin
    if local_path.nil?
      @sftp.download! remote_path
    else
      @sftp.download! remote_path, local_path
    end
  rescue
    puts "Error downloading file."
    return false
  end
  return true

end

#download_recursive(remote_path, local_path) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/lib/ssh_operations.rb', line 47

def download_recursive(remote_path, local_path)
  begin
    @sftp.download! remote_path, local_path, :recursive => true
  rescue
    $log.error "Failed to download #{remote_path}."
    return false
  end
end

#execute(command) ⇒ Object



39
40
41
# File 'lib/lib/ssh_operations.rb', line 39

def execute(command)
  @ssh.exec! command
end

#file?(path) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
117
118
119
120
# File 'lib/lib/ssh_operations.rb', line 114

def file? path
  begin
    @sftp.stat!(path).file?
  rescue
    false
  end
end

#file_exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
92
# File 'lib/lib/ssh_operations.rb', line 84

def file_exists? path
  begin
    @sftp.stat!(path)
    return true
  rescue
    return false
  end

end

#launch(path) ⇒ Object



94
95
96
# File 'lib/lib/ssh_operations.rb', line 94

def launch path
  @ssh.exec path
end

#launch_app(command, app) ⇒ Object



130
131
132
133
# File 'lib/lib/ssh_operations.rb', line 130

def launch_app command, app
  puts "#{command} \"#{app}\""
  self.execute("#{command} \"#{app}\"")
end

#list_dir(dir) ⇒ Object



75
76
77
# File 'lib/lib/ssh_operations.rb', line 75

def list_dir dir
  @sftp.dir.entries(dir).map {|x| x.name}
end

#list_dir_full(dir) ⇒ Object



79
80
81
# File 'lib/lib/ssh_operations.rb', line 79

def list_dir_full dir
  @sftp.dir.entries(dir)
end

#mkdir(path) ⇒ Object



102
103
104
# File 'lib/lib/ssh_operations.rb', line 102

def mkdir path
  @sftp.mkdir path
end

#mtime(path) ⇒ Object



122
123
124
# File 'lib/lib/ssh_operations.rb', line 122

def mtime path
  Time.new @sftp.stat!(path).mtime
end

#open(path) ⇒ Object



126
127
128
# File 'lib/lib/ssh_operations.rb', line 126

def open path
  Launchy.open path
end

#upload(local_path, remote_path) ⇒ Object



71
72
73
# File 'lib/lib/ssh_operations.rb', line 71

def upload(local_path, remote_path)
  @sftp.upload! local_path, remote_path
end