Class: Knj::SSHRobot
Defined Under Namespace
Classes: Forward
Instance Method Summary collapse
- #close ⇒ Object
- #exec(command) ⇒ Object (also: #shellCMD)
- #fileExists(filepath) ⇒ Object
- #forward(args) ⇒ Object
-
#initialize(args) ⇒ SSHRobot
constructor
A new instance of SSHRobot.
- #session ⇒ Object
- #session_spawn ⇒ Object
- #sftp ⇒ Object (also: #getSFTP)
- #shell ⇒ Object (also: #getShell)
- #sudo_exec(sudo_passwd, command) ⇒ Object
Constructor Details
Instance Method Details
#close ⇒ Object
71 72 73 74 |
# File 'lib/knj/sshrobot/sshrobot.rb', line 71 def close @session.close if @session @session = nil end |
#exec(command) ⇒ Object Also known as: shellCMD
27 28 29 |
# File 'lib/knj/sshrobot/sshrobot.rb', line 27 def exec(command) return self.session.exec!(command) end |
#fileExists(filepath) ⇒ Object
53 54 55 56 57 |
# File 'lib/knj/sshrobot/sshrobot.rb', line 53 def fileExists(filepath) result = self.exec("ls #{Strings.UnixSafe(filepath)}").strip return true if result == filepath return false end |
#forward(args) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/knj/sshrobot/sshrobot.rb', line 59 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
10 11 12 13 |
# File 'lib/knj/sshrobot/sshrobot.rb', line 10 def session @session = self.session_spawn if !@session return @session end |
#session_spawn ⇒ Object
15 16 17 |
# File 'lib/knj/sshrobot/sshrobot.rb', line 15 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
23 24 25 |
# File 'lib/knj/sshrobot/sshrobot.rb', line 23 def sftp @sftp = Net::SFTP.start(@args[:host], @args[:user], @args[:passwd], :port => @args[:port].to_i) end |
#shell ⇒ Object Also known as: getShell
19 20 21 |
# File 'lib/knj/sshrobot/sshrobot.rb', line 19 def shell return self.session.shell.sync end |
#sudo_exec(sudo_passwd, command) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/knj/sshrobot/sshrobot.rb', line 31 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 |