pexpect:无法使用命名空间在多跳节点上运行命令

时间:2022-08-23 08:57:51

I had posted this problem earlier https://*.com/questions/29794260/python-fabric-multi-hop-ssh-to-run-cmd-on-remote-host-using and since I was unable to find a fix I adopted pexpect to implement. However I am unable to achieve the intent of running command on the end machine:

我之前发布过这个问题https://*.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)

Here is the piece of code which I am running:

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

#!/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

The script does not error out, however it does not seem to access Node2 as no output on print is seen. Any help much appreciated

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

1 个解决方案

#1


Changed child.expect('$') to child.expect('\$'), resolved the issue

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

#1


Changed child.expect('$') to child.expect('\$'), resolved the issue

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