2

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

eth3とで正常に動作しますwlan3ppp0、モバイルブロードバンドモデムで動作する場合、MACアドレスが表示されません。

4

1 に答える 1

3

pppMAC(ハードウェア)アドレスを使用しません:

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                */
于 2011-12-26T00:08:41.310 に答える