本文主要利用python的相关模块进行批量ping ,测试IP连通性。
下面看具体代码(python3):
#!/usr/bin/env python
#-*-coding:utf-8-*-
import re
import subprocess
from io import StringIO
import multiprocessing
import time
import sys
def check_alive(ip):
result = subprocess.call('ping -w 1000 -n 1 %s' %ip,stdout=subprocess.PIPE,shell=True)
if result == 0:
h = subprocess.getoutput('ping ' + ip)
returnnum = h.split('平均 = ')[1]
info = (' 33[32m%s 33[0m 能ping通,延迟平均值为:%s' %(ip,returnnum))
print(' 33[32m%s 33[0m 能ping通,延迟平均值为:%s' %(ip,returnnum))
#return info
else:
with open('notong.txt','a') as f:
f.write(ip)
info = (' 33[31m%s 33[0m ping 不通!' % ip)
#return info
print(' 33[31m%s 33[0m ping 不通!' % ip)
if __name__ == '__main__':
print("开始批量ping所有IP!")
with open('ip.txt', 'r') as f: #ip.txt为本地文件记录所有需要检测连通性的ip
for i in f:
p = multiprocessing.Process(target=check_alive, args=(i,))
p.start()