如何捕获pexpect sendline命令输出并将其重定向到日志文件?

2024-09-27 21:28:24 发布

您现在位置:Python中文网/ 问答频道 /正文

当我在shell中手动运行下面的命令时

[root@Rhel67GA-Valencia ~]# /DPDK2/testpmd -c 3 -n 3 -d /DPDK2/librte_pmd_oce.so -w 0000:01:00.00 -w 0000:01:00.01 -- -i --nb-cores=1 --nb-ports=2

我在终端上得到以下输出:

^{pr2}$

当我尝试使用pexpect模块在python脚本中运行相同的命令时,我在终端上看不到任何输出,但是这个命令被成功地执行了。在

我需要将上面的命令输出捕获到日志文件中。在

下面是我正在使用的脚本

sample.py:

cmd = "/DPDK2/testpmd -c 3 -n 3 -d /DPDK2/librte_pmd_oce.so -w 0000:01:00.00     -w 0000:01:00.01 -- -i --nb-cores=1 --nb-ports=2"

child = pexpect.spawn(cmd)


child.expect("testpmd>")


child.sendline('start')

print "\ntestpmd> start \n"

time.sleep(30)


child.sendline('stop')

print "\ntestpmd> stop \n"


child.sendline('quit')

time.sleep(30)

Tags: 命令脚本child终端soportscorespexpect
1条回答
网友
1楼 · 发布于 2024-09-27 21:28:24

在每次调用child.sendline之前调用child.expect

child.expect("testpmd>")
child.sendline('start')

child.expect("testpmd>")
child.sendline('stop')

child.expect("testpmd>")
child.sendline('quit')

child.interact()

相关问题 更多 >

    热门问题