import subprocess
import threading
def is_reacheable(ip):
return_code = subprocess.call(['ping', ip])
if retcode == 1:
print('IP:%s is unreachable' % (ip))
else:
print('IP:%s is reachable' % (ip))
def main():
with open("ips.txt") as f: # ips.txt 存储IP地址
lines = f.readlines()
threads = []
for line in lines:
thr = threading.Thread(target=is_reacheable,args=(line,))
thr.start()
threads.append(thr)
for thr in threads:
thr.join()
if __name__ == '__main__':
main()
subprocess.
call
(args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None, **other_popen_kwargs)
运行由 args 所描述的命令。 等待命令完成,然后返回 returncode 属性。
需要捕获 stdout 或 stderr 的代码应当改用 run():
run(...).returncode