在python中获取移动宽带调制解调器的MAC地址

发布于 2024-12-23 00:16:40 字数 522 浏览 1 评论 0 原文

我想获取 Linux 上网络接口的 MAC 地址。我正在使用带有 PyQt4 模块的 Python。当我运行此代码时:

form PyQt4.QtNetwork import *
def getinfo():
  all_info = QNetworkInterface().allInterfaces()
  for interface in all_info:
        print interface.hardwareAddress()," ",interface.humanReadableName()

我得到以下输出:

00:00:00:00:00:00   lo
00:26:2D:8C:8F:B3   eth3
F0:7B:CB:3B:82:2B   wlan3
  ppp0

它与 eth3wlan3 配合良好,但与在我的移动宽带调制解调器上运行的 ppp0 配合使用,MAC地址不出现。

I want to get the MAC address for my network interface on Linux. I am using Python with PyQt4 modules. When I run this code:

form PyQt4.QtNetwork import *
def getinfo():
  all_info = QNetworkInterface().allInterfaces()
  for interface in all_info:
        print interface.hardwareAddress()," ",interface.humanReadableName()

I get this outout:

00:00:00:00:00:00   lo
00:26:2D:8C:8F:B3   eth3
F0:7B:CB:3B:82:2B   wlan3
  ppp0

It works fine with eth3 and wlan3, but with ppp0 which runs on my mobile broadband modem, the MAC address doesn't appear.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

川水往事 2024-12-30 00:16:40

ppp 不使用 MAC(硬件)地址:

ppp0      Link encap:Point-to-Point Protocol
          inet addr:172.27.42.1  P-t-P:172.27.42.17  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
          RX packets:416 errors:0 dropped:0 overruns:0 frame:0
          TX packets:397 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:3
          RX bytes:253765 (247.8 Kb)  TX bytes:62621 (61.1 Kb)

一般来说,如果您正在寻找 Python 中的网络接口管理,那么 netifaces。它被设计为在 Mac OS X、Linux 和 Windows 上跨平台。

>>> import netifaces as ni
>>> ni.interfaces()
['lo', 'eth0', 'eth1', 'vboxnet0', 'dummy1']
>>> ni.ifaddresses('eth0')
{17: [{'broadcast': 'ff:ff:ff:ff:ff:ff', 'addr': '00:02:55:7b:b2:f6'}], 2: [{'broadcast': '24.19.161.7', 'netmask': '255.255.255.248', 'addr': '24.19.161.6'}], 10: [{'netmask': 'ffff:ffff:ffff:ffff::', 'addr': 'fe80::202:55ff:fe7b:b2f6%eth0'}]}
>>> 
>>> ni.ifaddresses.__doc__
'Obtain information about the specified network interface.\n\nReturns a dict whose keys are equal to the address family constants,\ne.g. netifaces.AF_INET, and whose values are a list of addresses in\nthat family that are attached to the network interface.'
>>> # for the IPv4 address of eth0
>>> ni.ifaddresses('eth0')[2][0]['addr']
'24.19.161.6'

用于索引协议的数字来自 /usr/include/linux/socket.h (在 Linux 中)...

#define AF_INET         2       /* Internet IP Protocol         */
#define AF_INET6        10      /* IP version 6                 */
#define AF_PACKET       17      /* Packet family                */

ppp doesn't use a MAC (hardware) address:

ppp0      Link encap:Point-to-Point Protocol
          inet addr:172.27.42.1  P-t-P:172.27.42.17  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
          RX packets:416 errors:0 dropped:0 overruns:0 frame:0
          TX packets:397 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:3
          RX bytes:253765 (247.8 Kb)  TX bytes:62621 (61.1 Kb)

In general, if you're looking for network interface management in Python, look no further than netifaces. It is designed to be cross-platform on Mac OS X, Linux, and Windows.

>>> import netifaces as ni
>>> ni.interfaces()
['lo', 'eth0', 'eth1', 'vboxnet0', 'dummy1']
>>> ni.ifaddresses('eth0')
{17: [{'broadcast': 'ff:ff:ff:ff:ff:ff', 'addr': '00:02:55:7b:b2:f6'}], 2: [{'broadcast': '24.19.161.7', 'netmask': '255.255.255.248', 'addr': '24.19.161.6'}], 10: [{'netmask': 'ffff:ffff:ffff:ffff::', 'addr': 'fe80::202:55ff:fe7b:b2f6%eth0'}]}
>>> 
>>> ni.ifaddresses.__doc__
'Obtain information about the specified network interface.\n\nReturns a dict whose keys are equal to the address family constants,\ne.g. netifaces.AF_INET, and whose values are a list of addresses in\nthat family that are attached to the network interface.'
>>> # for the IPv4 address of eth0
>>> ni.ifaddresses('eth0')[2][0]['addr']
'24.19.161.6'

The numbers used to index protocols are from /usr/include/linux/socket.h (in Linux)...

#define AF_INET         2       /* Internet IP Protocol         */
#define AF_INET6        10      /* IP version 6                 */
#define AF_PACKET       17      /* Packet family                */
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文