使用pexpect,我希望生成一个ssh会话,并且ssh通过几个服务器连接到最终服务器,就像我使用Expect所做的那样。
Python版本: 2.7.3
#!/usr/bin/python
import pexpect
#import getpass
#import time
child = pexpect.spawn ('ssh foo@foo')
child.expect ('P*')
child.sendline ('blahblahblah')
child.expect ('P*')
child.sendline ('ssh server2.foo')
child.expect ('P*')
child.sendline ('blahblahblah')
child.interact() 我到达我的第一个跃点没有问题,登录到服务器foo,但我不知道pexpect语法期望在随后的跃点中使用"send -- ssh $user@$host \r“。
发布于 2015-04-18 14:02:08
您可以继续使用sendline,就像您已经做的那样
child = pexpect.spawn('ssh %s@%s' % (username, relayhost0), timeout = 5)
child.expect('P*')
child.sendline('ssh %s@%s' % (username, relayhost1))
child.expect('P*')
child.sendline('ssh %s@%s' % (username, relayhost2))
child.expect('P*')
child.interact()https://stackoverflow.com/questions/29683291
复制相似问题