我正在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])
注释掉调用可以得到预期的输出结果:
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
参数是ping
抱怨的。
我会非常感激任何帮助,特别是关于正确使用call()
的方面。
更新:
添加了我正在尝试模拟的bash脚本:
#!/bin/bash
for ip in $(seq 1 2); do
ping -c 1 192.168.68.$ip &
done
call([path, flags.strip(), ip_addr])
- undefined