Putting nested bash command in Python
This question already has an answer here:
$(..)
, aka command expansion, is performed by the shell. Since you're now using Python instead of a shell, you don't get that feature for free.
The simple fix is to invoke a shell and give it your command:
tcpd = subprocess.Popen(["bash", "-c", "timeout 2 tcpdump -i eth1 -s 96 -w /usr/src/pcapFiles/dump$(date +%y%m%d-%H%M%S).pcap"], stdout=subprocess.PIPE)
output, err = tcpd.communicate()
The arguably more correct fix is to get the current date in Python:
import datetime
filename=datetime.datetime.now().strftime("/usr/src/pcapFiles/dump%y%m%d-%H%M%S.pcap")
tcpd = subprocess.Popen(["timeout", "2", "tcpdump", "-i", "eth1", "-s", "96", "-w", filename, stdout=subprocess.PIPE)
output, err = tcpd.communicate()
链接地址: http://www.djcxy.com/p/13508.html
上一篇: 在python解释器中运行shell命令
下一篇: 将嵌套的bash命令放入Python中