开发者

Router backup script

开发者 https://www.devze.com 2023-01-11 06:25 出处:网络
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 y

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号