簡體   English   中英

如何捕獲pexpect sendline命令輸出並將其重定向到日志文件?

[英]how to capture pexpect sendline command output and redirect it to 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腳本中運行相同的命令時,我在終端上看不到任何輸出,但命令已成功執行。

我需要將以上命令輸出捕獲到日志文件中。

下面是我正在使用的腳本

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)

在每次調用child.sendline之前,先致電child.expect

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

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

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

child.interact()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM