Class: Cisco::SSH

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/cisco/ssh.rb

Instance Attribute Summary

Attributes included from Common

#host, #password, #prompt

Instance Method Summary collapse

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(options)
  @host    = options[:host]
  @user    = options[:user]
  @password = options[:password]
  @prompt  = options[:prompt]
  @sshargs = options[:directargs] || [@host, @user, {:password => @password, :auth_methods => ["password"]}]
  @pwprompt = options[: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

#runObject



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