zoukankan      html  css  js  c++  java
  • 「python」: arp脚本的两种方法

    「python」: arp脚本的两种方法

    第一种是使用arping工具:

    #!/usr/bin/env python
    import subprocess
    import sys
    import re
    
    def arping(ipaddress = "192.168.1.1"):
        p = subprocess.Popen("/usr/sbin/arping -c 2 %s" % ipaddress, shell = True,
                                stdout = subprocess.PIPE)
        out = p.stdout.read()
        result = out.split()
        for item in result:
            if ":" in item:
                print item
    
    if __name__ == "__main__":
        if len(sys.argv) > 1:
            for ip in sys.argv[1:]:
                print "arping", ip
                arping(ip)
        else:
            arping()

    这种方法依赖于特定平台的工具,整体就是使用了一个系统调用。

    第二种是使用scapy,这种方法可以实现平台无关化

    #!/usr/bin/env python
    from scapy import srp,Ether,ARP,conf
    import sys
    
    def arping(iprange="192.168.1.1/24"):
        conf.verb = 0
        ans,unans = srp(Enter(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=iprange),
                        timeout=2)
        collection = []
        for snd, rcv in ans:
            result = rcv.sprintf(r"%ARP.psrc% %Ether.src%").split()
            collection.append(result)
        return collection
    
    if __name__ == "__main__":
        if len(sys.argv) > 1:
            for ip in sys.argv[1:]:
                print "arping", ip
                print arping(ip)
        else:
            print arping()

    scapy编写简单的ARP扫描脚本 本课程基于 Python 的 scapy 模块编写,适合有 Python 基础的同学学习,最终完成一个简单的 ARP 扫描脚本。

  • 相关阅读:
    JSP学习-10-EL表达式
    深入浅出Mybatis(一)
    第10章—开启事务
    第09章—使用Lombok插件
    第08章—整合Spring Data JPA
    第06章—热部署
    第05章—Swagger2打造在线接口文档
    第03章—打造RESTful风格API
    第04章—整合Mybatis
    第01章—快速构建
  • 原文地址:https://www.cnblogs.com/timssd/p/4658960.html
Copyright © 2011-2022 走看看