Python - 仅获取连接的本地 NIC 的 MAC 地址



目标是收集连接的本地网卡的MAC地址,不是所有本地 NIC 的列表:)

通过使用socketconnect (to_a_website),我可以用getsockname()来获取IP,用于连接到互联网。

但是从 IP 中如何获取本地网卡的 MAC 地址?

该问题的主要原因是是否有多个 NIC。

正如vartec建议的那样netifaces应该可以从IP->iface很好地工作:

    import netifaces as nif
    def mac_for_ip(ip):
        'Returns a list of MACs for interfaces that have given IP, returns None if not found'
        for i in nif.interfaces():
            addrs = nif.ifaddresses(i)
            try:
                if_mac = addrs[nif.AF_LINK][0]['addr']
                if_ip = addrs[nif.AF_INET][0]['addr']
            except IndexError, KeyError: #ignore ifaces that dont have MAC or IP
                if_mac = if_ip = None
            if if_ip == ip:
                return if_mac
        return None

测试:

    >>> mac_for_ip('169.254.90.191')
    '2c:41:38:0a:94:8b'

使用 netifaces 模块。它也在 PyPI 上,因此您可以通过 easy_installpip 安装它。

您无法检索外部 IP 的 MAC 地址。

有关更多说明,请参阅如何在 C# 中获取外部 IP 的 mac 地址中的讨论。

执行此操作的一种原始方法是使用操作系统上可用的命令行工具。 使用 subprocess 模块(不是 os.system() !)运行该工具,收集输出并解析它。

在 Windows 上,所需的命令是 ipconfig /all

在大多数Unices上,包括Linux,OSX和BSD,它ifconfig

可能有更好的方法可以做到这一点,而无需掏出命令行实用程序,但我不知道......然而

Windows XP 上的 ipconfig /all 输出示例:

D:Documents and SettingsLAYip>ipconfig /all
Windows IP Configuration
        Host Name . . . . . . . . . . . . : <redacted>
        Primary Dns Suffix  . . . . . . . : <redacted>
        Node Type . . . . . . . . . . . . : Hybrid
        IP Routing Enabled. . . . . . . . : No
        WINS Proxy Enabled. . . . . . . . : No
        DNS Suffix Search List. . . . . . : <redacted>
                                            <redacted>
Ethernet adapter Local Area Connection:
        Connection-specific DNS Suffix  . : <redacted>
        Description . . . . . . . . . . . : Intel(R) 82579LM Gigabit Network Con
nection #2
        Physical Address. . . . . . . . . : 5C-26-0A-60-8D-C7
        Dhcp Enabled. . . . . . . . . . . : Yes
        Autoconfiguration Enabled . . . . : Yes
        IP Address. . . . . . . . . . . . : xxx.xxx.28.29
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . : xxx.xxx.28.254
        DHCP Server . . . . . . . . . . . : xxx.xxx.23.13
        DNS Servers . . . . . . . . . . . : xxx.xxx.23.13
                                            xxx.xxx.23.11
        Lease Obtained. . . . . . . . . . : Thursday, 12 April 2012 9:14:41 AM
        Lease Expires . . . . . . . . . . : Friday, 20 April 2012 9:14:41 AM
Ethernet adapter VirtualBox Host-Only Network:
        Connection-specific DNS Suffix  . :
        Description . . . . . . . . . . . : VirtualBox Host-Only Ethernet Adapter
        Physical Address. . . . . . . . . : 08-00-27-00-28-E6
        Dhcp Enabled. . . . . . . . . . . : No
        IP Address. . . . . . . . . . . . : 192.168.56.1
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . :

Linux 下的ifconfig输出:

lws@helios:~$ ifconfig
eth0      Link encap:Ethernet  HWaddr 00:25:22:db:8c:b6
          inet addr:10.1.1.2  Bcast:10.1.1.255  Mask:255.255.255.0
          inet6 addr: fe80::225:22ff:fedb:8cb6/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:322333 errors:0 dropped:0 overruns:0 frame:0
          TX packets:296952 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:40005876 (40.0 MB)  TX bytes:162343969 (162.3 MB)
          Interrupt:40 Base address:0x4000
lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:362 errors:0 dropped:0 overruns:0 frame:0
          TX packets:362 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:31806 (31.8 KB)  TX bytes:31806 (31.8 KB)

您无法从套接字捕获 mac 地址。您需要一个以太网帧,该帧可以在 TCP 处理的最低层找到 chain.to 需要监视(捕获)网络流量,请通过解析数据包的标头来查找一些数据包。 并从中提取所需的信息,例如 MAC 地址。

这是有用的代码跨度,可以帮助您做到这一点。

获取系统

mac ID 的另一种迂回方法是使用 ping 命令 ping 系统名称,然后执行 arp - 针对被 ping 的 IP 地址的请求。 这样做的缺点是您需要将 ping 响应写入 Python 的内存并执行 readline 操作以检索 IP 地址,然后在写入时将相应的 ARP 数据写入内存系统名称、IP 地址和 MAC ID 到显示器或测试文件。

我试图做一些类似于系统验证检查的事情,以提高测试过程的自动化,并且脚本暂时在python中。

最新更新