python forkping

2023-01-31 00:01:52 python forkping

#!/usr/bin/env python3
import os

import subprocess

def ping(host):
  result = subprocess.call('ping -c2 %s &> /dev/null'%host,shell=True)
  if result == 0:
        print('%s:up'%host)
  else:
        print('%s:down'%host)

 if __name__=='__main__':
   ips = [ '192.168.25.%s'%i for i in range(1,255)]
   for ip in ips:
     pid = os.fork()  
    if not pid:       #使用子进程工作
       ping(ip)
       exit()     #每个子进程工作完exit一次,否则子进程会再生成子进程

相关文章