python-ping多个ip
单线程:
import subprocess
import time
rack1_pdu_ip = ["192.168.244.138", "192.168.244.138", "192.168.244.138"]
rack2_pdu_ip = ["192.168.244.158", "192.168.244.158", "192.168.244.158"]
rack3_pdu_ip = ["192.168.244.178", "192.168.244.178", "192.168.244.178"]
def choose_ip(first, end):
rack_pdu_ip = []
for ip in range(first, end + 1):
rack_pdu_ip.append(eval("rack" + str(first) + "_pdu_ip"))
first += 1
return rack_pdu_ip
def ping_ip(first, end):
start_time = time.time()
for ip_list in choose_ip(first, end):
print(" ")
print('RACK' + str(first))
first += 1
for ip in ip_list:
res = subprocess.call('ping -n 2 -w 5 %s' % ip, stdout=subprocess.PIPE)
print(ip, "\033[1;32m True \033[0m" if res == 0 else "\033[1;31m False \033[0m")
print('执行所用时间:%s' % (time.time() - start_time))
ping_ip(1, 3) # ping_ip(1, 3)代表从"RACK1"到"RACK3"
多线程:
from ping3 import ping
from concurrent.futures import ThreadPoolExecutor
ips = ["192.168.244.138", "192.168.244.138", "192.168.244.138",
"192.168.244.158", "192.168.244.158", "192.168.244.158",
"192.168.244.178", "192.168.244.178", "192.168.244.178"]
def pings(ips):
ips_status = dict()
with ThreadPoolExecutor(max_workers=500) as pool:
results = pool.map(ping, ips)
for index, result in enumerate(results):
ip = ips[index]
if type(result) == float:
ips_status[ip] = True
else:
ips_status[ip] = False
return ips_status
ips_status = pings(ips)
# print(ips_status)
i = 0
for key, value in ips_status.items():
if i % 3 == 0:
print("\n" + "RACK" + " " + str(int((i + 3) / 3)))
print(key, "\033[1;32m True \033[0m" if value == 1 else "\033[1;31m False \033[0m")
i += 1