【问题标题】:pexpect ssh through multiple hostspexpect ssh 通过多个主机
【发布时间】:2015-04-18 14:02:08
【问题描述】:

我想使用 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 上登录,但我不知道后续跃点期望“send -- ssh $user@$host \r”的 pexpect 语法。

【问题讨论】:

    标签: python ssh pexpect


    【解决方案1】:

    您可以像以前那样继续使用 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()
    

    【讨论】:

      猜你喜欢
      • 2015-11-27
      • 1970-01-01
      • 2012-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多