Python扫描内网存活的主机
2015-08-29 18:18
609 查看
#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()
相关文章推荐
- python2.7 beautifulsoup学习使用
- python2.7 beautifulsoup安装下载
- Python分词模块推荐:jieba中文分词
- python内置函数汇总(2)
- python类型转换、数值操作
- Python 函数之函数定义、调用、传参
- Python找回MySQL密码
- Python之包管理工具
- Python中的编码问题
- 理解Python中的with…as…语法
- python 类和对象的属性
- python 有关super
- Python爬虫框架Scrapy教程(1)—入门
- Python - 带参数的方法
- Python爬虫框架Scrapy教程(1)—入门
- 2015/8/28 Python基础(2):对象
- 2015/8/26 Python基础(1):基本规则及赋值
- python - os模块中比较有用的部分
- Python使用os.listdir()函数来得目录内容的介绍
- python大法好!