I have the following code that connects to my router just fine. The problem is that once connected, I try to pass the "sh ver" command th开发者_JS百科at never gets passed to the router. Thanks for your help!
require 'net/telnet'
cisco = '1.1.1.1' #Enter the IP address here
user = 'admin' #Enter username here
pass = 'mypass' #Enter password here
tn = Net::Telnet::new('Host' => cisco, 'prompt' => /^\Username:/ )
tn.cmd('String'=>'admin', 'Match'=>/Password:/) { |c| puts c }
tn.cmd(pass) { |c| puts c }
------------------Does not work below this line---------------------
tn.cmd('String'=>'sh ver')
The problem is that you set 'prompt' to an expression that matches Username: (caveat: you have a backslash there, so it probably actually matches SERNAME:)
So when you do tn.cmd(pass) it sends the password then is waiting for Username: (or SERNAME:).
Change 'prompt' to a regex that matches your cisco's usual prompt (the prompt you see after successfully logging in).
So this is the code that I used based on your recommendations that works. Thanks
require 'net/telnet'
tn = Net::Telnet::new("Host" => "1.1.1.1", "Timeout" => 10000, "Prompt" => /[$%#>] \z/n)
tn.cmd('String' =>'admin' , 'Match'=>/Password:/) { |c| puts c } tn.cmd('String' =>'pass', 'Match'=>/#/) { |c| puts c } tn.cmd('String' =>'terminal length 0', 'Match'=>/#/) { |c| puts c } tn.cmd('String'=>'sh run', 'Match'=>/#/) { |c| puts c } tn.close
精彩评论