Class: Knj::SSHRobot
Defined Under Namespace
Classes: Forward
Instance Method Summary collapse
- #close ⇒ Object
-
#exec(command, &block) ⇒ Object
(also: #shellCMD)
Executes a command.
- #fileExists(filepath) ⇒ Object
- #forward(args) ⇒ Object
-
#initialize(args) ⇒ SSHRobot
constructor
A new instance of SSHRobot.
-
#session ⇒ Object
Spawns a session if it hasnt already been spawned and returns it.
-
#session_spawn ⇒ Object
Spawns a new net-ssh-instance.
- #sftp ⇒ Object (also: #getSFTP)
-
#shell ⇒ Object
(also: #getShell)
Returns the a shell-session.
-
#sudo_exec(sudo_passwd, command) ⇒ Object
Executes a command as “root” via “sudo”.
Constructor Details
#initialize(args) ⇒ SSHRobot
Returns a new instance of SSHRobot.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/knj/sshrobot/sshrobot.rb', line 2 def initialize(args) require "net/ssh" @forwards = [] @args = Knj::ArrayExt.hash_sym(args) @args[:port] = 22 if !@args.key?(:port) if block_given? begin yield(self) ensure self.close end end end |
Instance Method Details
#close ⇒ Object
89 90 91 92 |
# File 'lib/knj/sshrobot/sshrobot.rb', line 89 def close @session.close if @session @session = nil end |
#exec(command, &block) ⇒ Object Also known as: shellCMD
Executes a command.
39 40 41 42 43 44 45 46 47 |
# File 'lib/knj/sshrobot/sshrobot.rb', line 39 def exec(command, &block) if block return self.session.exec!(command) do |channel, stream, line| block.call(:channel => channel, :stream => stream, :line => line) end else return self.session.exec!(command) end end |
#fileExists(filepath) ⇒ Object
71 72 73 74 75 |
# File 'lib/knj/sshrobot/sshrobot.rb', line 71 def fileExists(filepath) result = self.exec("ls #{Strings.UnixSafe(filepath)}").strip return true if result == filepath return false end |
#forward(args) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/knj/sshrobot/sshrobot.rb', line 77 def forward(args) Knj::ArrayExt.hash_sym(args) args[:type] = "local" if !args[:type] args[:session] = self.session_spawn if !args[:session] args[:host_local] = "0.0.0.0" if !args[:host_local] return SSHRobot::Forward.new(args) end |
#session ⇒ Object
Spawns a session if it hasnt already been spawned and returns it.
19 20 21 22 |
# File 'lib/knj/sshrobot/sshrobot.rb', line 19 def session @session = self.session_spawn if !@session return @session end |
#session_spawn ⇒ Object
Spawns a new net-ssh-instance.
25 26 27 |
# File 'lib/knj/sshrobot/sshrobot.rb', line 25 def session_spawn return Net::SSH.start(@args[:host], @args[:user], :password => @args[:passwd], :port => @args[:port].to_i) end |
#sftp ⇒ Object Also known as: getSFTP
34 35 36 |
# File 'lib/knj/sshrobot/sshrobot.rb', line 34 def sftp @sftp = Net::SFTP.start(@args[:host], @args[:user], @args[:passwd], :port => @args[:port].to_i) end |
#shell ⇒ Object Also known as: getShell
Returns the a shell-session.
30 31 32 |
# File 'lib/knj/sshrobot/sshrobot.rb', line 30 def shell return self.session.shell.sync end |
#sudo_exec(sudo_passwd, command) ⇒ Object
Executes a command as “root” via “sudo”. Accepts the “sudo”-password and a command.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/knj/sshrobot/sshrobot.rb', line 50 def sudo_exec(sudo_passwd, command) result = "" self.session.open_channel do |ch| ch.request_pty ch.exec("sudo #{command}") do |ch, success| ch.on_data do |ch, data| if data =~ /^\[sudo\] password for (.+):\s*$/ ch.send_data("#{sudo_passwd}\n") else result << data end end end end self.session.loop return result end |