我正在尝试编写一个脚本,我需要使用ssh多个跳转服务器来访问路由器。
例如LocalMachine ---- ssh --- server1 --- ssh --- server2 --- telnet / ssh --- router
不确定这样做的最佳方式是什么,我试图通过pexpect实现它。 我的要求是当我从服务器server2 telnet到路由器并且它超时/连接被拒绝我想尝试ssh 有什么建议吗?
import time, pexpect
child = pexpect.spawn('ssh username@server-1') ##########ssh to 1st Jump server
child.expect('password: ')
child.sendline('abc')
child.expect('$')
child.sendline('ssh username@server-2') ##########ssh to 2nd Jump server
print child.before
child.expect('password:')
child.sendline('xyz')
child.expect('$')
print child.before
host=raw_input("Enter Router name: ")
try:
print "Trying Telnet ", host
child.sendline(' telnet ' + host) ### router telnet at this point i want if telnet is timeout/connection refused try ssh
print child.before
except pexpect.TIMEOUT:
print child.before
else:
child.expect(':')
child.sendline("User")
child.expect(":")
child.sendline('passwprd')
child.expect('#')
child.sendline("\n")
child.expect("#")
finally:
print "Trying SSH ", host
child.sendline(' ssh -l User ' + host)
print child.before
child.expect(":")
child.sendline('password')
child.expect('#')
child.sendline("\n")
print child.before
child.interact()
答案 0 :(得分:1)
计算机上运行的程序实例将引发BMI BMI = new BMI();
textBox1.Text = BMI.val;
异常。它不知道您已经ssh的远程计算机上运行的pexpect.TIMEOUT
实例是否超时。您可以看到telnet连接是否有超时或类似错误的唯一方法,即。连接被拒绝,是通过telnet
查看进程的输出。如果您查看pexpect
的{{3}},它表示您可以提供条件列表 - 这可能是一种方法。
例如:
expect