我正在Linux下编写一个简单的python脚本,以使用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])
注释掉 call 输出预期:
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
与我期望的不同。我注意到ping抱怨-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'])
我无法在我的环境中复制“双重打印”。
关于python - python subprocess.call参数问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37370951/