定时检查ip存活性并记录到表格中

Posted by wukaiqiang; tagged with none

cat /root/batchping.py

#!/usr/bin/python3

# -*- coding: utf-8 -*-

import time, subprocess, threading

#from queue import Quese

time_start = time.time()

iplist = open('/root/aixiplist.txt','r')

errorlist = []



def checkalive(ip):

  status = subprocess.call('ping -c 2 %s' % ip, shell=True,stdout=subprocess.PIPE)

  if status == 0:

    pass

  elif status == 1:

    print('无法连接'+ ip)

    errorlist.append(ip)



#thread

if __name__ == '__main__':

  print('---------------------')

  lt = time.asctime(time.localtime(time.time()))

  print('当前时间',lt)

  threads = []

  for ip in iplist:

    t = threading.Thread(target=checkalive,args=(ip,))

    t.start()

    threads.append(t)     

  for t in threads:

    t.join()

  if len(errorlist) >= 1:

    print(errorlist,'down')

  if len(errorlist) == 0:

    print('all pong')

  time_end = time.time()

  print('检测完毕,耗时',round(time_end - time_start,2),'秒')