Class: Cisco::SSH
Instance Attribute Summary
Attributes included from Common
Instance Method Summary collapse
-
#close(chn) ⇒ Object
Disconnect the session.
- #cmd(cmd, prompt = nil, &block) ⇒ Object
-
#initialize(options) ⇒ SSH
constructor
A new instance of SSH.
- #run ⇒ Object
Methods included from Common
#clear_cmd, #clear_init, #enable, #extra_init
Constructor Details
#initialize(options) ⇒ SSH
Returns a new instance of SSH.
9 10 11 12 13 14 15 16 17 |
# File 'lib/cisco/ssh.rb', line 9 def initialize() @host = [:host] @user = [:user] @password = [:password] @prompt = [:prompt] @sshargs = [:directargs] || [@host, @user, {:password => @password, :auth_methods => ["password"]}] @pwprompt = [:pwprompt] || "Password:" @cmdbuf, @extra_init = [], [] end |
Instance Method Details
#close(chn) ⇒ Object
Disconnect the session. Either Net::SSH has a bug, or the Cisco implementation is sometimes whacky, causing an exception to be thrown when trying to close the channel via the SSH protocol. To get around this, this method simply sends “exit” until the device disconnects us.
50 51 52 53 54 |
# File 'lib/cisco/ssh.rb', line 50 def close(chn) 10.times do chn.send_data("exit\n") unless (!chn.active? || chn.closing?) end end |
#cmd(cmd, prompt = nil, &block) ⇒ Object
19 20 21 |
# File 'lib/cisco/ssh.rb', line 19 def cmd(cmd, prompt = nil, &block) @cmdbuf << [cmd + "\n", prompt, block] end |
#run ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/cisco/ssh.rb', line 23 def run @inbuf = "" @results = [] @ssh = Net::SSH.start(*@sshargs) @ssh.open_channel do |chan| chan.send_channel_request("shell") do |ch, success| if !success abort "Could not open shell channel" else ch.on_data do |chn, data| @outblock.call(data) if @outblock @inbuf << data check_and_send(chn) end (@cmdbuf = [] and yield self) if block_given? @cmdbuf.insert(0, *@extra_init) if @extra_init.any? end end end @ssh.loop @results end |