发布时间:2019-07-29 10:01:39编辑:auto阅读(1799)
#!/usr/bin/env python
import pexpect
import sys
host="146.11.85.xxx"
user="inteuser"
password="xxxx"
command="ls -l"
child = pexpect.spawn('ssh -l %s %s %s'%(user, host, command))
child.expect('password:')
child.sendline(password)
childlog = open('promp.log',"ab") #文件属性必须为二进制写+,否则会报错
child.logfile = childlog
child.expect(pexpect.EOF)#如果子进程结束了,你再去child.expect(pattern)会报EOF错误,模块提供了一种方法,child.expect(pexpect.EOF),不会报错,如果子进程结束了返回0
childlog.close()
#!/usr/bin/env python
import pexpect
import sys
host="146.11.85.xxx"
user="inteuser"
password="xxxx"
command="ls -l"
child = pexpect.spawn('ssh -l %s %s %s'%(user, host, command))
child.expect('password:')
child.sendline(password)
__console__ = sys.stdout #备份当前的标准输出到命令行
childlog = open('promp.log',"w") #这里文件属性不能为二进制,否则报错TypeError: a bytes-like object is required, not 'str'
sys.stdout = childlog #将childlog设为标准输出的对像
child.expect(pexpect.EOF)
print(child.before.decode()) #这里使用decode()函数,将输出的目录信息格式化
#child.before 这个值包含文本从头一直到匹配的位置
childlog.close()
sys.stdout = __console__ #还原标准输出对象为命令行
上一篇: Python-使用watchdog热更新
下一篇: Python3 使用urllib 发送a
48876
47943
38724
35865
30292
27048
26075
20911
20717
19076
515°
613°
618°
620°
597°
581°
650°
718°
839°
948°