zoukankan      html  css  js  c++  java
  • Python脚本扫描给定网段的MAC地址表(scapy或 python-nmap)

    目录

    用scapy模块写

    用 python-nmap 模块写


    python3.7  windows环境

    以下两个都可以扫描指定主机或者指定网段的 IP 对应的 MAC 地址,然后保存到 csv 文件中。用scapy模块写的脚本速度很快,几秒就可以全部扫描完,用python-nmap扫描的比较慢,建议用 scapy 模块写的。

    用scapy模块写

    -H 参数可以是一个ip地址,也可以是一个网段。例:192.168.10.100  或   192.168.10.10-20 或  192.168.10.0/24

    # -*- coding: utf-8 -*-
    """
    Created on Wed Nov  7 22:27:55 2018
    @author: 小谢
    """
    from scapy.all import *
    import csv
    import optparse
    import time
    def Ip_Mac(host):
        try:
            ans,unans=srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=host),timeout=2,verbose=False)  #发送ARP请求包,verbose=False的话,就不显示发包信息
        except Exception as e:
            print("异常对象的类型是:%s"%type(e))
            print("异常对象的内容是:%s"%e)
        else:
            try:
                with open("ip.csv","w",newline="") as f:
                     csv_writer=csv.writer(f)
                     csv_writer.writerow(('IP','MAC'))
                     for send,rec in ans:
                         ip_mac=rec.sprintf("{ARP:%ARP.psrc%-%Ether.src%}")   #将包按照固定的格式打印
                         print(ip_mac)
                         mac=ip_mac.split("-")          #去除中间的 - ,返回元组型数据
                         csv_writer.writerow(mac)       #将元组写入到文件中
            except Exception as e:
                 print("异常对象的类型是:%s"%type(e))
                 print("异常对象的内容是:%s"%e)
            finally:
                f.close()     
    def main():
        usage="python %proc -H 192.168.10.10 / 192.168.10.0/24"
        parser=optparse.OptionParser(usage)
        parser.add_option('-H',dest='Hosts',type='string',help='target hosts')
        (options,args)=parser.parse_args()
        Hosts=options.Hosts
        if Hosts==None:
            print(parser.usage)
            exit(0)
        else:
            Ip_Mac(Hosts)
    main()

    既可以扫描一个ip地址,也可以扫描一个网段 

     用 python-nmap 模块写

    这个模块扫描特别慢,不建议使用。

    -H 参数可以是一个IP地址或者一个网段。例:192.168.10.100 或192.168.10.10-20 或  192.168.10.0/24

    # -*- coding: utf-8 -*-
    """
    Created on Sun Nov  4 19:58:51 2018
    @author: 小谢
    """
    ##列出给定网段的主机的MAC地址表
    import nmap
    import optparse
    import time
    import csv
    def ListMac(hosts):
        nm=nmap.PortScanner()
        nm.scan(hosts)
        targets=[]
        for t in nm.all_hosts():
            if nm[t]['status']['state']=="up":
                mac=nm[t]['addresses']['mac']
                print("ip: %s  mac: %s"%(t,mac))
                targets.append((t,mac))
            else:
                pass
        return targets
    
    def main():
        usage="python %proc -H 192.168.10.10-20"
        parser=optparse.OptionParser(usage)
        parser.add_option('-H',dest='Hosts',type='string',help='target hosts')
        (options,args)=parser.parse_args()
        Hosts=options.Hosts
        if Hosts==None:
            print(parser.usage)
            exit(0)
        else:
            target=ListMac(Hosts)
            try:
                with open("ip.csv","w",newline="") as f:
                    csv_writer=csv.writer(f)
                    csv_writer.writerow(('IP','MAC'))
                    for i in target:
                        csv_writer.writerow(i)
            except Exception as e:
                    print("异常对象的类型是:%s"%type(e))
                    print("异常对象的内容是:%s"%e)
            finally:
                   f.close()
    if __name__=='__main__':
        main()

    相关文章: Python中Scapy网络嗅探模块的使用

                       Python中python-nmap模块的使用

  • 相关阅读:
    《七哥说道》第十八章:何处不风雨,套路说江湖
    《七哥说道》第十七章:北漂青年,人海茫茫
    《闲扯Redis四》List数据类型底层编码转换
    《闲扯Redis三》Redis五种数据类型之List型
    《闲扯Redis二》String数据类型之底层解析
    《闲扯Redis一》五种数据类型之String型
    Js解析Json数据获取元素JsonPath与深度
    《七哥说道》第十六章:程序员,江湖见
    Swagger2.9.2进入API界面报NumberFormatException异常
    绝顶高手必经之路【资源共享】
  • 原文地址:https://www.cnblogs.com/csnd/p/11807814.html
Copyright © 2011-2022 走看看