美文网首页python自动化运维
实践:局域网活动ip扫描

实践:局域网活动ip扫描

作者: 如来自然 | 来源:发表于2016-01-14 21:31 被阅读190次

    [root@www dns]# cat SJ3.py

    !/usr/bin/env python

    import platform
    import sys
    import os
    import time
    import thread

    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()

    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):
    for i in range(1,256):
    ip = '%s.%s'%(ip_prefix,i)
    thread.start_new_thread(ping_ip, (ip,))
    time.sleep(0.2)

    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()

    [root@www dns]#

    测试:

    脚本后面跟上网段
    [root@www dns]# python SJ3.py 192.168.1.0
    start time Mon Jan 11 11:38:50 2016
    ip: 192.168.1.1 is ok ***
    ip: 192.168.1.104 is ok ***
    ip: 192.168.1.106 is ok ***
    ip: 192.168.1.141 is ok ***
    ip: 192.168.1.140 is ok ***
    Do you want to ping broadcast? Then -b
    end time Mon Jan 11 11:39:41 2016

    相关文章

      网友评论

        本文标题:实践:局域网活动ip扫描

        本文链接:https://www.haomeiwen.com/subject/bphhkttx.html