路由器备份脚本
我有以下代码可以很好地连接到我的路由器。问题是,一旦连接,我尝试传递“sh ver”命令,但该命令永远不会传递到路由器。感谢您的帮助!
需要“网络/远程登录”
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')
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 that 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')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您将“提示”设置为与用户名匹配的表达式:(警告:那里有一个反斜杠,所以它可能实际上与 SERNAME 匹配:)
所以当您执行 tn.cmd(pass) 时,它会发送密码,然后正在等待对于用户名:(或 SERNAME:)。
将“提示”更改为与您的思科常用提示(成功登录后看到的提示)相匹配的正则表达式。
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).
所以这是我根据您的建议使用的有效代码。感谢
require 'net/telnet'
tn = Net::Telnet::new("Host" => "1.1.1.1",
“超时”=> 10000,
“提示”=> /[$%#>] \z/n)
tn.cmd('String' =>'admin' , 'Match'=>/密码:/) { |c|把 c }
tn.cmd('String' => 'pass', 'Match'=>/#/) { |c|把 c }
tn.cmd('String' =>'终端长度0', 'Match'=>/#/) { |c|把 c }
tn.cmd('String'=>'sh run', 'Match'=>/#/) { |c|把 c }
tn.关闭
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