python - pexpect脚本执行无报错,但是没有执行结果
本文介绍了python - pexpect脚本执行无报错,但是没有执行结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
python脚本中使用pexpect模块实现远程SSH登录,登陆成功后显示/home目录文件清单,并通过日志文件记录所有的输入与输出。
#!/usr/bin/env python
import pexpect
import sys
child=pexpect.spawn('ssh xxxxx@192.168.42.2')
fout=file('mylog1.txt','w')
child.logfile=fout
child.expect("password:")
child.sendline("xxxxx")
child.expect('$')
child.sendline('ls /home')
运行结果:
xxxxx:pythontest/ $ python simple1.py
xxxxx:pythontest/ $ cat mylog1.txt
xxxxxx@192.168.42.2's password: xxxxxx
ls /home
期望运行结果:
xxxxx:pythontest/ $ cat mylog1.txt
xxxxxx@192.168.42.2's password: xxxxxx
ls /home
xxx xxxx.zshrc
问题以解决:child.expect(pexpect.EOF)
解决方案
在最后一行下面加:
child.expect(pexpect.EOF)
执行成功
这篇关于python - pexpect脚本执行无报错,但是没有执行结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文