「python」: arp脚本的两种方法
2015-07-19 17:44
621 查看
「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 扫描脚本。
相关文章推荐
- python教程,文章list
- python教程,文章list
- Python如何实现单例模式?其他23种设计模式python如何实现?
- 解读python手册的例子a, b = b, a+b
- 2.7 请写出一段Python代码实现删除一个list里面的重复元素
- Python中的闭包
- python脚本语言2
- 2.6 Python里面如何实现tuple和list的转换?
- 2.4 Python是如何进行类型转换的?
- 2.3 解释一下python的 and-or 语法
- 【python learning】python的虚拟环境-virtualenv
- [2015-07-18] python004
- python题目3
- 2.1 Python是如何进行内存管理的?
- 《python核心编程》笔记——文件的创建、读取和显示
- Scrapy中用cookie模拟登陆新浪微博
- 菜鸟学习OpenCV(Python)碰到的一些小问题
- python题目2
- python的7个经典的面试题
- Windows下如何构建和发布Python模块