Python3实现局域网存活主机扫描
import os
import time
import platform
import threading
def get_os():
'''''
get os 类型
'''
os = platform.system()
if os == "Windows":
return "n"
else:
return "c"
def ping_ip(ip_str):
cmd = ["ping", "-{op}".format(op=get_os()),
"1", ip_str]
output = os.popen(" ".join(cmd)).readlines()
flag = False
for line in list(output):
if not line:
continue
if str(line).upper().find("TTL") >=0:
flag = True
break
if flag:
print("ip: %s is ok ***"%ip_str)
def find_ip(ip_prefix):
'''''
给出当前的127.0.0 ,然后扫描整个段所有地址
'''
for i in range(1,256):
ip = '%s.%s'%(ip_prefix,i)
t = threading.Thread(target=ping_ip, args=(ip,)) # 注意传入的参数一定是一个元组!
t.start()
if __name__ == "__main__":
print("start time %s"%time.ctime())
ip_prefix = "10.1.130"
find_ip(ip_prefix)
print("end time %s"%time.ctime())
评论 (0)