Python获取指定网段正在使用的IP
【摘要】 #!/usr/bin/env python# -*- coding: utf-8 -*- '''''使用方法样例 python test20.py 192.168.1.1(会扫描192.168.1.1-255的ip)多线程加速后大概十几秒'''import platformimport sysimport osimport timeimport threading live_ip = 0 ...
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''''
使用方法样例 python test20.py 192.168.1.1
(会扫描192.168.1.1-255的ip)
多线程加速后大概十几秒
'''
import platform
import sys
import os
import time
import threading
live_ip = 0
def 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()
for line in output:
if str(line).upper().find("TTL") >= 0:
print("ip: %s is ok ***" % ip_str)
global live_ip
live_ip += 1
break
def find_ip(ip_prefix):
'''''
给出当前的127.0.0 ,然后扫描整个段所有地址
'''
threads = []
for i in range(1, 256):
ip = '%s.%s' % (ip_prefix, i)
threads.append(threading.Thread(target=ping_ip, args={ip, }))
for i in threads:
i.start()
for i in threads:
i.join()
if __name__ == "__main__":
print("start time %s" % time.ctime())
cmd_args = sys.argv[1:]
args = "".join(cmd_args)
ip_pre = '.'.join(args.split('.')[:-1])
find_ip(ip_pre)
print("end time %s" % time.ctime())
print('本次扫描共检测到本网络存在%s台设备' % live_ip)
|

【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)