html tool

2016年5月13日星期五

pexpect使用


问题:
cpp文件中的print shell输出无法在如下的stdout和stderr获得
        self.app_log_b = subprocess.Popen([cmd], shell=True,  stdout = subprocess.PIPE, stdin = subprocess.PIPE)
        # Send the data and get the output
        stdout, stderr = self.app_log_b.communicate(getchar)

解决:

# -*- coding:utf8 -*-
# http://oychw.iteye.com/blog/2264416
# [popexizhi] 当前测试只在linux上通过
import pexpect 
# 发起ssh连接到10.6.0.232的子进程,使用root用户 
child = pexpect.spawn('ssh root@10.6.0.232') 
child.expect('password:') # 等待password:字符出现 
print child.before + child.after # 输出password:前后的字符 
child.sendline('password')    # 发送密码 
child.expect(']#')  # 等待]#字符出现 
print child.before + child.after 
child.interact() # 把ssh的连接交给用户控制


[popexizhi]
突然发现python的库好酷:)

没有评论:

发表评论