python subprocess.call参数的问题

我正在写一个简单的Python脚本,在Linux下使用subprocess对我的子网上的主机执行一批并发的单个ping。

手动执行该命令: ping -c 1 192.168.68.1

剧本::

 #!/usr/bin/python from subprocess import call path = "ping" flags = "-c 1 " for i in range(1,3): ip_addr = "192.168.68." + str(i) args = flags + ip_addr print "doing: {} {}".format(path, args) call([path, args]) 

评论呼叫输出预期:

 doing: ping -c 1 192.168.68.1 doing: ping -c 1 192.168.68.2 

通过call() ,脚本似乎调用ping但未知参数。 输出如下:

 doing: ping -c 1 192.168.68.1 Usage: ping [-aAbBdDfhLnOqrRUvV64] [-c count] [-i interval] [-I interface] [-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos] [-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option] [-w deadline] [-W timeout] [hop1 ...] destination Usage: ping -6 [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface] [-l preload] [-m mark] [-M pmtudisc_option] [-N nodeinfo_option] [-p pattern] [-Q tclass] [-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option] [-w deadline] [-W timeout] destination doing: ping -c 1 192.168.68.2 Usage: ping [-aAbBdDfhLnOqrRUvV64] [-c count] [-i interval] [-I interface] [-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos] [-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option] [-w deadline] [-W timeout] [hop1 ...] destination Usage: ping -6 [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface] [-l preload] [-m mark] [-M pmtudisc_option] [-N nodeinfo_option] [-p pattern] [-Q tclass] [-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option] [-w deadline] [-W timeout] destination 

看来ping被每次迭代都调用了两次,并且有不同的args 。 我注意到了这个-6说法。
我将不胜感激任何帮助,尤其是正确使用call()

更新:
添加bash脚本我试图模拟:

 #!/bin/bash for ip in $(seq 1 2); do ping -c 1 192.168.68.$ip & done 

传递给函数call的参数应该是列表中的单个元素,如下所示:

 call(['ping','-c','1','192.168.68.2']) 

我无法在我的环境中重现“双重印刷”。

正如@Pablo提到的flagspath变量应该是一个列表元素,所以在调用call方法之前,只需要将你的命令字符串转换为列表:

 path = "ping" flags = "-c 1" cmd_list = [path] # convert string to list cmd_list = cmd_list + flags.split(' ') # converting your flags string to list elements call(cmd_list)