码迷,mamicode.com
首页 > 编程语言 > 详细

[PythonCode]扫描局域网的alive ip地址

时间:2018-03-14 16:58:30      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:port   bre   pos   otto   博客   应用   htm   put   tool   

内网的主机都是自己主动分配ip地址,有时候须要查看下有那些ip在使用,就写了个简单的脚本。

linux和windows下都能够用,用多线程来ping1-255全部的地址,效率不高。2分钟左右。 先凑合和用吧。


#-*- coding: utf-8 -*-
#author: orangleliu  date: 2014-11-12
#python2.7.x  ip_scaner.py

‘‘‘
不同平台,实现对所在内网端的ip扫描

有时候须要知道所在局域网的有效ip,可是又不想找特定的工具来扫描。

用法 python ip_scaner.py 192.168.1.1 (会扫描192.168.1.1-255的ip) ‘‘‘ import platform import sys import os import time import thread 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) thread.start_new_thread(ping_ip, (ip,)) time.sleep(0.3) if __name__ == "__main__": print "start time %s"%time.ctime() commandargs = sys.argv[1:] args = "".join(commandargs) ip_prefix = ‘.‘.join(args.split(‘.‘)[:-1]) find_ip(ip_prefix) print "end time %s"%time.ctime()


是应用的时候: python ip_scaner.py 192.168.31.1 就会扫描 1-255全部的ip地址了。

D:\CodeHouse\python\tools>python ip_scaner.py 10.0.1.38
start time Wed Nov 12 18:50:58 2014
ip: 10.0.1.1 is ok ***
ip: 10.0.1.2 is ok ***
ip: 10.0.1.24 is ok ***
ip: 10.0.1.38 is ok ***
ip: 10.0.1.39 is ok ***
end time Wed Nov 12 18:52:16 2014

就这样。


本文出自?“orangleliu笔记本”?博客。转载请务必保留此出处http://blog.csdn.net/orangleliu/article/details/41048173

作者: orangleliu?


[PythonCode]扫描局域网的alive ip地址

标签:port   bre   pos   otto   博客   应用   htm   put   tool   

原文地址:https://www.cnblogs.com/zhchoutai/p/8568193.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!