class Knj::SSHRobot

Public Class Methods

new(args) { |self| ... } click to toggle source
# 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

Public Instance Methods

close() click to toggle source
# File lib/knj/sshrobot/sshrobot.rb, line 89
def close
  @session.close if @session
  @session = nil
end
exec(command, &block) click to toggle source

Executes a command.

# 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
Also aliased as: shellCMD
fileExists(filepath) click to toggle source
# 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) click to toggle source
# 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
getSFTP()
Alias for: sftp
getShell()
Alias for: shell
session() click to toggle source

Spawns a session if it hasnt already been spawned and returns it.

# File lib/knj/sshrobot/sshrobot.rb, line 19
def session
  @session = self.session_spawn if !@session
  return @session
end
session_spawn() click to toggle source

Spawns a new net-ssh-instance.

# 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() click to toggle source
# 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
Also aliased as: getSFTP
shell() click to toggle source

Returns the a shell-session.

# File lib/knj/sshrobot/sshrobot.rb, line 30
def shell
  return self.session.shell.sync
end
Also aliased as: getShell
shellCMD(command, &block)
Alias for: exec
sudo_exec(sudo_passwd, command) click to toggle source

Executes a command as “root” via “sudo”. Accepts the “sudo”-password and a command.

# 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