如何从接口获取物理接口ip地址?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (21)

到目前为止,我使用PyQt类所做的工作:

all_Addresses = QNetworkInterface.allAddresses()    #list-of-QHostAddress

for addr in all_Addresses:
    print(addr.toString())

输出:

172.16.0.186 - Virtual Interface IP address
192.168.10.2 - Physical interface IP address. I want this one.
127.0.0.1

使用socket:

import socket
print(socket.gethostbyname(socket.gethostname()))

输出:

172.16.0.186 - When openVPN is on
192.168.10.2 - When its off
  1. 有什么办法区分他们吗?
  2. 这可以用普通的Python来完成,而不是使用PyQt类吗?
  3. 我怎样才能得到IPv 6地址呢?
提问于
用户回答回答于

你应该使用netifaces。它被设计为跨平台的,并且包含专用于Windows的代码以及在不同类UNIX / UNIX平台上工作的各种通用版本。

由于netifaces版本0.10.0,Python3支持。

使用摘要

>>> from netifaces import AF_INET, AF_INET6, AF_LINK, AF_PACKET, AF_BRIDGE
>>> import netifaces as ni
>>> ni.interfaces()
['lo', 'eth0', 'eth1', 'vboxnet0', 'dummy1']
>>>
>>> ni.ifaddresses('eth0')[AF_LINK]   # NOTE: AF_LINK is an alias for AF_PACKET
[{'broadcast': 'ff:ff:ff:ff:ff:ff', 'addr': '00:02:55:7b:b2:f6'}]
>>> ni.ifaddresses('eth0')[AF_INET]
[{'broadcast': '172.16.161.7', 'netmask': '255.255.255.248', 'addr': '172.16.161.6'}]
>>>
>>> # eth0 ipv4 interface address
>>> ni.ifaddresses('eth0')[AF_INET][0]['addr']
'172.16.161.6'
>>>>

细节

Windows支持:

大多数MS Windows安装都不需要编译器。如果你收到有关安装MS Visual C ++ for Windows的警告,请非常小心,因为你需要将用于Python的编译器版本与用于该模块的版本相匹配。

netifaces数据结构的详细示例:

>>> 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': '172.16.161.7',
            'netmask': '255.255.255.248',
            'addr': '172.16.161.6'
        }
    ],
    10: [
        {
            'netmask': 'ffff:ffff:ffff:ffff::',
            'addr': 'fe80::202:55ff:fe7b:b2f6%eth0'
        }
    ]
}
>>> 
>>> print(ni.ifaddresses.__doc__)
Obtain information about the specified network interface.

Returns a dict whose keys are equal to the address family constants,
e.g. netifaces.AF_INET, and whose values are a list of addresses in
that family that are attached to the network interface.
>>>
>>> # for the IPv4 address of eth0
>>> ni.ifaddresses('eth0')[2][0]['addr']
'172.16.161.6'

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

我的3.2内核有他们在这里:/usr/src/linux-headers-3.2.0-4-common/include/linux/socket.h

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

好消息是,你不必记住所有这些头常量,它们包含在netifaces中

>>> from netifaces import AF_INET, AF_INET6, AF_LINK, AF_PACKET, AF_BRIDGE
>>> import netifaces as ni
用户回答回答于

使用Linux SIOCGIFADDR ioctl查找与网络接口相关联的IP地址,给定该接口的名称,例如“eth0“。地址作为一个字符串返回,该字符串包含一个虚线四边形。

import socket
import fcntl
import struct

def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])
    )[20:24])

>>> get_ip_address('lo')
'127.0.0.1'

>>> get_ip_address('eth0')
'38.113.228.130'

想要更多

扫码关注云+社区