pexpect是Python语言的类Expect实现,pexpect的使用,围绕3个关键命令做操作:
首先用 spawn 来执行一个程序
然后用 expect 来等待指定的关键字,这个关键字是被执行的程序打印到标准输出上面的
最后当发现这个关键字以后,根据关键字用 send 方法来发送字符串给这个程序
#!/usr/bin/python #-*- coding: utf-8 -*- import pexpect import sys import time child = pexpect.spawn('/data/ceshi.py') #将pexpect的输入输出信息写到mylog.txt文件中 fout = file('/root/mylog.txt','w') child.logfile = fout child.expect (':') child.sendline('yes') child.expect(':') child.sendline('') child.expect(':') child.sendline('') child.expect(':') child.sendline('') child.expect(':') child.sendline('yes') child.expect(pexpect.EOF) #打印输出日志 print child.before
参考文档:
https://www.cnblogs.com/zz27zz/p/7918717.html
转载于:https://blog.51cto.com/hellvenus/2153983