Python 2.7 使用 Scapy 发送 SYN 及获取 MAC 地址方法汇总

唐可
唐可
发布时间:2026-10-13| 5人浏览过
原创

本文介绍在 Python 2.7 环境下,利用 Scapy 模块构造并发送 SYN 包的实例,以及通过 nmap、系统命令 arp、Scapy ARP 请求和 arpreq 模块等多种方式获取同一网段内设备的 MAC 地址与网卡类型。文章对比了各方法的优缺点,并提供了使用 mac.py 库解析厂商信息的代码示例,适用于 Windows 和 Linux 系统。

Python 2.7 Scapy 发送 SYN 包实例

以下是具体的代码实现部分。


from scapy.all import *
 
def syn():
 scrIP = '192.168.10.29'
 srcPort = 23345
 desIP = '12.39.27.23'
 desPort = 8000
 
 ip = IP(src=scrIP, dst=desIP)
 tcp = TCP(sport=srcPort, dport=desPort, seq=13131342, flags='S')
 pkg = ip/tcp
 
 # c->s syn
 res = sr1(pkg)
 res.display()

Python 获取 MAC 地址与网卡类型的多种方法

补充知识:利用 Python 通过 IP 获取 MAC 地址及网卡类型(仅限同一网段)

根据网络搜索,目前主要有以下几种实现方式。

测试环境涵盖 Windows 和 Linux 系统。

方法一:使用 nmap,需通过 python-nmap 库调用,这要求机器上已安装 nmap 工具。

使用 nmap 获取主机信息


import nmap
nm=nmap.PortScanner()
nm.scan('xxx.xxx.xxx.xxx','xx') # ip地址和端口,端口不填也可以
a=nm['xxx.xxx.xxx.xxx'] #返回主机的详细信息
print(a)
##################################
{'status': {'state': 'up', 'reason': 'arp-response'}, 
'hostnames': [{'type': 'PTR', 'name': 'bogon'}], 
'vendor': {'00:0C:29:F6:2B:F0': 'VMware'}, 
'addresses': {'mac': '00:0C:29:F6:2B:F0', 'ipv4': 'xxx.xxx.xxx.xxx'}, 
'tcp': {111: {'product': 'Microsoft Windows 7 - 10 microsoft-ds', 'state': 'open', 'version': '', 'name': 'microsoft-ds', 'conf': '10', 'extrainfo': 'workgroup: WORKGROUP', 'reason': 'syn-ack', 'cpe': 'cpe:/o:microsoft:windows'}}}

这种方式获取结果非常简便,但耗时较长,因此不建议在生产环境中频繁使用。

方法二:通过控制台执行 arp -a 命令。

通过系统命令 arp 获取 MAC 地址


def output_cmd(command):
 r = os.popen(command)
 content = r.read()
 r.close()
 return content


def arp_command(ip_address):
 ping_cmd = "ping " + ip_address + " -n 2 "
 result = output_cmd(ping_cmd)
 find_ttl = result.find("TTL")
 if find_ttl != -1:
  arp_cmd = "arp -a %s" % ip_address
  arp_result = output_cmd(arp_cmd)
  ip2 = ip_address + " [ ]+([\w-]+)"
  ip2_mac = re.findall(ip2, arp_result)
  if len(ip2_mac):
   return ip2_mac[0]
  else:
   return 0
 else:
  result = u'无人使用的ip'
 return result

该流程需先执行 ping 操作,随后通过 arp -a [IP] 获取 ARP 表。获取数据后,需自行使用正则表达式匹配 MAC 地址。示例中展示的是 Windows 下的匹配逻辑,Linux 系统需根据实际输出格式调整正则规则。

方法三:利用 Scapy 模块(前提是该机器未禁用 IPv6)。

使用 Scapy 发送 ARP 请求


from scapy.all import *

arp_pkt = Ether(dst="ff:ff:ff:ff:ff:ff") / ARP(pdst=ip)
res = srp1(arp_pkt, timeout=1, verbose=0)
print {"localIP": res.psrc, "mac": res.hwsrc}

方法四:通过 arpreq 模块。

使用 arpreq 模块


[root@oradb ~]# python
Python 2.7.5 (default, Aug 4 2017, 00:39:18) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import arpreq
>>> arpreq.arpreq('192.168.xx.xxx')
'xx:xx:xx:xx:xx:xx'

在通过上述任一方法获取到 MAC 地址后,可进一步获取网卡类型信息。

可以直接使用 mac.py 库进行厂商信息匹配。

安装命令:pip install mac.py

使用 mac.py 解析网卡厂商信息


from macpy import Mac

mac = Mac()
information = mac.search(00-11-F1-01-01)
print information 

若发现内置信息较为陈旧,可手动从 IEEE 官网下载最新的 MAC 地址与厂商对应文件,进行本地比对。

以上即为 Python 2.7 使用 Scapy 发送 SYN 及获取 MAC 地址的完整内容,希望能对读者有所帮助,欢迎支持本站。

相关标签:

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。