在ruby的telnet类中使用基于横幅的user和pass

在ruby的telnet类中使用基于横幅的user和pass,ruby,cmd,automation,telnet,Ruby,Cmd,Automation,Telnet,我想编写一个脚本,根据ruby中Net::Telnet建立的连接的横幅输出,使用用户名和密码登录: 登录和执行一些命令的代码(命令在“命令”文件中): 所需:相同的脚本,但在登录尝试完成时(就在您必须插入用户ans密码之前)输出的路由器横幅/MOTD中显示“My2ndroter”时使用第二个参数。如果场景正确,您可以使用waitfor对数据进行虹吸,直到收到接收用户名的提示。然后处理waitfor块中的数据,以检测要使用的两组参数中的哪一组 routers = [ { "user" =>

我想编写一个脚本,根据ruby中Net::Telnet建立的连接的横幅输出,使用用户名和密码登录:

登录和执行一些命令的代码(命令在“命令”文件中):


所需:相同的脚本,但在登录尝试完成时(就在您必须插入用户ans密码之前)输出的路由器横幅/MOTD中显示“My2ndroter”时使用第二个参数。

如果场景正确,您可以使用
waitfor
对数据进行虹吸,直到收到接收用户名的提示。然后处理waitfor块中的数据,以检测要使用的两组参数中的哪一组

routers = [
  { "user" => "myuser", "pass" => "mypass", "enablepass" => "myendablepass" },
  { "user" => "my2ndUser", "pass" => "my2ndPass", "enablepass" => "my2ndEndablePass" }
]

tn = Net::Telnet::new("Host" => line,
"Timeout" => 10,
"Waittime" => 0.1,
"Prompt" => /[#>:]/n) { |resp| print "==> "+resp}

my_router = routers[0]
# Replace /login:/ with whatever regexp that matches your login prompt, e.g. your catch-all /[#>:]/n
tn.waitfor(/login:/) { |banner| my_router = routers[1] if banner.match("my2ndrouter") }

tn.cmd(my_router['user']) { |c| print c }
tn.cmd(my_router['pass']) { |c| print c }
tn.cmd("terminal length 0") { |c| print c }
tn.cmd("en") { |c| print c }
tn.cmd(my_router['enablepass']) { |c| print c }
tn.cmd("\n")  { |c| print c }

File.open('commands').each do |l|
tn.cmd(l) { |c| print c }
end
tn.cmd("\n")  { |c| print c }
tn.cmd("\n")  { |c| print c }


tn.close

好的,当我在学院里把这个解决方案改为“用户”=>“我的用户”时,这就行了。。。等等现在我看到了登录,用户被插入,但它挂起了。(这是另一个问题doh):)
routers = [
  { "user" => "myuser", "pass" => "mypass", "enablepass" => "myendablepass" },
  { "user" => "my2ndUser", "pass" => "my2ndPass", "enablepass" => "my2ndEndablePass" }
]

tn = Net::Telnet::new("Host" => line,
"Timeout" => 10,
"Waittime" => 0.1,
"Prompt" => /[#>:]/n) { |resp| print "==> "+resp}

my_router = routers[0]
# Replace /login:/ with whatever regexp that matches your login prompt, e.g. your catch-all /[#>:]/n
tn.waitfor(/login:/) { |banner| my_router = routers[1] if banner.match("my2ndrouter") }

tn.cmd(my_router['user']) { |c| print c }
tn.cmd(my_router['pass']) { |c| print c }
tn.cmd("terminal length 0") { |c| print c }
tn.cmd("en") { |c| print c }
tn.cmd(my_router['enablepass']) { |c| print c }
tn.cmd("\n")  { |c| print c }

File.open('commands').each do |l|
tn.cmd(l) { |c| print c }
end
tn.cmd("\n")  { |c| print c }
tn.cmd("\n")  { |c| print c }


tn.close