import threading
from subprocess import getoutput
# 获取单个主机丢包率信息
def get_pkg_loss(ip):
cmd = "ping -c 2 -w 3 {}".format(ip)
ret = getoutput(cmd)
for line in ret.splitlines():
if "loss" in line:
*_,loss, _, _ = line.split()
print(loss)
else:
print("主机不通")
# 创建子线程
thread_list = []
with open("ip-list.txt") as f:
for ip in f:
t = threading.Thread(target=get_pkg_loss, args=(ip,))
thread_list.append(t)
for t in thread_list:
t.start()
for t in thread_list:
t.join()