路由器备份脚本

发布于 2024-09-13 22:52:25 字数 509 浏览 2 评论 0原文

我有以下代码可以很好地连接到我的路由器。问题是,一旦连接,我尝试传递“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫烠⑼条掵仅有一顆心 2024-09-20 22:52:25

问题是您将“提示”设置为与用户名匹配的表达式:(警告:那里有一个反斜杠,所以它可能实际上与 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).

爱人如己 2024-09-20 22:52:25

所以这是我根据您的建议使用的有效代码。感谢

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文