到 telnet 切换的 Ruby 脚本 [英] Ruby script to telnet switch

查看:33
本文介绍了到 telnet 切换的 Ruby 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了以下 ruby​​ 脚本,该脚本可以 telnet 到 Cisco 设备,该 telnet 到 Cisco 设备并运行命令show int status err".

需要'net/telnet'C3550_20_PterraEst = "192.168.244.20" #这里输入IP地址USER = "user" #这里输入用户名PASS = "password" #这里输入密码ENABLE = "password" #这里输入启用密码打印 "Selezionare il 钢琴 [0-1-2-All]:";# 从控制台获取输入,val1 = 获取;tn = Net::Telnet::new("Host" => C3550_20_PterraEst,超时"=>5、提示"=>/用户名/)tn.cmd("\n#{用户}")tn.cmd(通过)tn.cmd("sh int status err\n") { |c|打印 c }出口

现在我希望那些 Cisco 设备输出(当我发送show int status err"时)应该分配给我脚本中的一个变量......我解释得更好:

假设命令'show int status err'返回这个值:

内部 fa0/20内部 fa0/25

我想做一些诸如... variable1 = 'Int fa0/20,Int fa0/25'然后在我的脚本中使用 variable1.

我可以用巫术吗?

解决方案

我觉得你想做的不是:

tn.cmd("sh int status err\n") { |c|打印 c }

而是:

response = tn.cmd("sh int status err\n")

根据 NET::Telnet 上的文档,Telnet#cmd 返回响应.如果你传递一个块,它也会产生对块的响应,但如果你想将响应存储在一个变量中,我的例子是最好的方法.

如果您还想打印对 STD 的响应,您应该可以同时执行这两种操作,如下所示:

response = tn.cmd("sh int status err\n") { |c|打印 c }

然而,如果 tn.cmd("sh int status err\n") { |c|print c } 给你一个错误,我的解决方案可能无法工作,直到你弄清楚为什么它会产生错误.如果您需要帮助解决该问题,请将其添加到评论中.

综上所述,如果您希望脚本生成额外的命令来响应 telnet 响应,您可能需要考虑使用 [expect][1].>

I've created the following ruby script that telnet to Cisco devices, that telnet to Cisco devices and run command 'show int status err'.

require 'net/telnet'

C3550_20_PterraEst = "192.168.244.20" #Enter the IP address here

USER = "user" #Enter username here

PASS = "password" #Enter password here

ENABLE = "password" #Enter enable password here

print "Selezionare il piano [0-1-2-All]: ";

# get the input from the console, 
val1 = gets;
tn = Net::Telnet::new("Host" => C3550_20_PterraEst,
"Timeout" => 5,
"Prompt" => /Username/ )
tn.cmd("\n#{USER}") 
tn.cmd(PASS) 
tn.cmd("sh int status err\n") { |c| print c }
exit

Now I want that those Cisco devices output (when I send 'show int status err') should be assigned to a variables inside my script...I explain better:

suppose that command 'show int status err' returns this value:

Int fa0/20 Int fa0/25

I want do something such as... variable1 = 'Int fa0/20,Int fa0/25' and later use variable1 inside my script.

In witch way can I do?

解决方案

I think what you want to do is not:

tn.cmd("sh int status err\n") { |c| print c }

but rather:

response = tn.cmd("sh int status err\n")

According to the docs on NET::Telnet, Telnet#cmd returns the response. If you pass it a block it also yields the response to the block, but if you want to store the response in a variable, my example is the best way to do that.

If you wanted to also print the response to STD out you should be able to do both, like this:

response = tn.cmd("sh int status err\n") { |c| print c }

However, if tn.cmd("sh int status err\n") { |c| print c } is giving you an error, my solution might not work until you figure out why it's producing an error. If you'd like help troubleshooting that, you should add it to the comments.

With all of that said, if you are hoping to have your script produce additional commands in response to the telnet response, you may want to look into using [expect][1].

这篇关于到 telnet 切换的 Ruby 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆