python forkping
程序员文章站
2022-05-18 23:01:58
#!/usr/bin/env python3import os import subprocess def ping(host): result = subprocess.call('ping -c2 %s &> /dev/null'%host,shell=True) if result == 0: ......
#!/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一次,否则子进程会再生成子进程
下一篇: 如何打造网站克隆、仿站工具(C#版)