如何捕获pexpect sendline命令输出并将其重定向到logfile?

时间:2016-03-17 17:54:47

标签: python pexpect logfile

当我在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

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

COMMAND OUTPUT Beginning:

Port 0: 00:90:FA:30:97:26
Configuring Port 1 (socket 0)
librte_pmd_oce:
oce_pf_dev_rx_start hw_vlan_strip = 0x1

Port 1: 00:90:FA:30:97:2A
Checking link statuses...
Port 0 Link Up - speed 10000 Mbps - full-duplex
Port 1 Link Up - speed 10000 Mbps - full-duplex
Done

testpmd> start             <=== USER INPUT ===>
io packet forwarding - CRC stripping disabled - packets/burst=32
nb forwarding cores=1 - nb forwarding ports=2
RX queues=1 - RX desc=128 - RX free threshold=0
RX threshold registers: pthresh=0 hthresh=0 wthresh=0
TX queues=1 - TX desc=512 - TX free threshold=0
TX threshold registers: pthresh=0 hthresh=0 wthresh=0
TX RS bit threshold=0 - TXQ flags=0x0

testpmd> stop                <=== USER INPUT ===>
Telling cores to stop...
Waiting for lcores to finish...

---------------------- Forward statistics for port 0  ----------------------
RX-packets: 24             RX-dropped: 0             RX-total: 24
TX-packets: 24             TX-dropped: 0             TX-total: 24
----------------------------------------------------------------------------

---------------------- Forward statistics for port 1  ----------------------
RX-packets: 24             RX-dropped: 0             RX-total: 24
TX-packets: 24             TX-dropped: 0             TX-total: 24
----------------------------------------------------------------------------

+++++++++++++++ Accumulated forward statistics for all ports+++++++++++++++
RX-packets: 48             RX-dropped: 0             RX-total: 48
TX-packets: 48             TX-dropped: 0             TX-total: 48  
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Done.

testpmd> quit                  <=== USER INPUT ===>
Stopping port 0...done
Stopping port 1...done
bye...
[root@Rhel67GA-Valencia ~]#

End of COMMAND OUTPUT

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

我需要将上面的COMMAND OUTPUT捕获到日志文件中。

下面是我正在使用的脚本

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)

1 个答案:

答案 0 :(得分:0)

在每次致电child.expect之前致电child.sendline

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

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

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

child.interact()