python - pexpect脚本执行无报错,但是没有执行结果
黄舟
黄舟 2017-04-18 09:45:07
0
2
960

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)

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

모든 응답(2)
小葫芦

마지막 줄 아래에 추가:
child.expect(pexpect.EOF)
실행 성공

洪涛

원단 사용:

http://www.fabfile.org/

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!