【问题标题】:pexpect: unable to run command on multi-hopped node using namespacepexpect:无法使用命名空间在多跳节点上运行命令
【发布时间】:2015-05-04 19:55:36
【问题描述】:

我之前在https://stackoverflow.com/questions/29794260/python-fabric-multi-hop-ssh-to-run-cmd-on-remote-host-using 发布过这个问题,由于我无法找到修复程序,所以我采用了 pexpect 来实现。但是我无法实现在终端机器上运行命令的意图:

TestMachine -- connected---Node1--(namespace ssh)--Node2(run cmd on Node2)

这是我正在运行的一段代码:

#!/usr/bin/env python
import pexpect
import sys
h1='8.8.8.2'
un='root'
### This below worked correctly as expected ###
child = pexpect.spawn("ssh root@node1")
child.expect('password:')
child.sendline('abc123')
child.expect('#')
child.sendline("ls")
child.expect('#')
print child.before
child.sendline('ssh root@node2')
child.expect('password:')
child.sendline('abc123')
child.expect('#')
child.sendline("ifconfig eth2")
child.expect('#')
print child.before
child.sendline('ip netns exec dhcp_ns_1 ssh test@20.20.20.2') #Node2 access
child.expect('password:')
child.sendline('abc123')
child.expect('')
child.sendline("ifconfig eth0")
child.expect('$')
print child.before

脚本没有出错,但它似乎没有访问 Node2,因为在打印时看不到任何输出。非常感谢任何帮助

【问题讨论】:

    标签: python pexpect


    【解决方案1】:

    child.expect('$')更改为child.expect('\$'),解决了问题

    【讨论】:

      猜你喜欢
      • 2017-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多