【问题标题】:Python - Get MAC address of only the connected local NIC [duplicate]Python - 仅获取连接的本地网卡的 MAC 地址 [重复]
【发布时间】:2012-04-13 08:30:00
【问题描述】:

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

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

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

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

【问题讨论】:

  • “连接的网卡”是什么意思?你怎么知道外部IP有MAC地址? MAC 地址是以太网的东西,而不是互联网的东西。
  • 还有“为什么要知道MAC地址”的问题。了解您的用例会有所帮助。
  • 不是重复的,链接的问题没有提供对 OP 特定问题的正确答案。

标签: python networking ethernet


【解决方案1】:

正如 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'

【讨论】:

    【解决方案2】:

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

    请参阅how to get mac address of external IP in C# 上的讨论以获取更多说明。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 为什么这被否决了?看起来像does exactly what the OP wanted - 获取接口的 MAC 地址。这个答案有我的投票。
      • @Li-aungYip:报复性投票,与这个问题完全无关
      • 你能给netifaces一个IP号码并得到MAC或设备名称作为回报吗?我查看了 netifaces 但找不到该功能,列出设备并列出设备上的 IP,但找不到“从 IP 到设备名称”或从“IP 到设备的 MAC 地址”
      【解决方案4】:

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

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

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

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

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

      D:\Documents and Settings\LAYip>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)
      

      【讨论】:

      • 是的,我发现我可以解析 ifconfig(我不使用 windows,任何 M$ 对你都不利:),只是认为可能有一种“更好”的方式来做到这一点。
      【解决方案5】:

      您无法从套接字中捕获 MAC 地址。您需要一个 ethernet frame,它可以在 tcp 处理链的最底层找到。要做到这一点,您需要监控(捕获)您的网络流量,找到一些数据包通过解析数据包的标头。并从中提取所需的信息,例如mac地址。

      这是useful code span,可以帮助您做到这一点。

      【讨论】:

        【解决方案6】:

        另一种获取系统 mac id 的迂回方法是使用 ping 命令 ping 系统名称,然后针对被 ping 的 IP 地址执行 arp -a 请求。这样做的缺点是你需要将 ping 响应写入 python 的内存并执行 readline 操作以检索 IP 地址,然后在写入系统名称、IP 地址和mac id 到有问题的机器到显示器或测试文件。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-10-07
          • 2017-11-17
          • 1970-01-01
          • 1970-01-01
          • 2016-02-17
          • 2011-10-09
          • 2011-07-13
          相关资源
          最近更新 更多