我想编写一个脚本,根据Net :: Telnet在ruby中建立的连接中的横幅输出,使用用户名和密码进行登录:
登录并执行一些命令的代码(命令在文件"命令")中:
USER = "myuser"
PASS = "mypass"
USER2 = "my2ndUser"
PASS2 = "my2ndPass"
ENABLEPASS = "myenablepass"
ENABLEPASS2 = "my2ndEnablePass"
tn = Net::Telnet::new("Host" => line,
"Timeout" => 10,
"Waittime" => 0.1,
"Prompt" => /[#>:]/n) { |resp| print "==> "+resp}
tn.cmd(USER) { |c| print c }
tn.cmd(PASS) { |c| print c }
tn.cmd("terminal length 0") { |c| print c }
tn.cmd("en") { |c| print c }
tn.cmd(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
所需:相同的脚本,但在说出" my2ndrouter"时使用第二个参数。在登录尝试完成时输出的路由器的banner / MOTD中(就在您必须插入用户和传递之前)
答案 0 :(得分:1)
如果我的方案正确,您可以使用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