ping ip multiprocessing applet

    Recently, environmental maintenance needs often need to determine whether the IPs on some servers are reachable. Due to the large number of servers, it is too cumbersome to manually ping check one by one. Write a small program to use.

from multiprocessing import Pool
import os, sys

ping_cfg = "ping_config.txt"

def ping_ip(ip):
    ping_result = os.system('ping -n 2 -w 3 %s > nul' % (ip,))
    if ping_result == 0:
        print "%s success" % (ip,)
    else:
        print "%s fail" % (ip,)

if __name__== '__main__':
    try:
       fp = open(ping_cfg, 'r')
       ip_list = fp.readlines()
       fp.close()
    except:
       print "%s file not exist." % (ping_cfg,)
       sys.exit(1)
    p = Pool()
    for ip in ip_list:
        p.apply_async(ping_ip, args=(ip.strip(),))
    p.close()
    p.join()

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324859642&siteId=291194637