前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 扫描内网存活主机

python 扫描内网存活主机

作者头像
py3study
发布2020-01-07 16:36:07
2K0
发布2020-01-07 16:36:07
举报
文章被收录于专栏:python3python3
代码语言:javascript
复制
#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()
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档