繁体   English   中英

如何从一个接口获取物理接口IP地址

[英]How to get the physical interface IP address from an interface

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

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

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

Output:

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()))

Output:

172.16.0.186 - When openVPN is on
192.168.10.2 - When its off
  1. 有没有办法区分它们?
  2. 这可以用普通的 Python 代替 PyQt 类来完成吗?
  3. 我怎样才能获得 IPv6 地址?

你应该使用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 安装不需要编译器。 如果您收到有关为 Windows 安装 MS Visual C++ 的警告,请非常小心,因为您需要将用于 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 kernel 在这里有它们: /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                */

好消息是您不必记住所有那些 header 常量,它们包含在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'

更多

我使用这个解决方案。 这实际上有点棘手,它只适用于 linux 系列。

import commands
intf = 'eth0'
intf_ip = commands.getoutput("ip address show dev " + intf).split()
intf_ip = intf_ip[intf_ip.index('inet') + 1].split('/')[0]
print intf_ip

这些代码在 linux 系列操作系统上使用ip命令。 它将 output 从ip命令中分离出来,并且只获取这些接口的 IPv4 地址。 您可以将值intf更改为eth1p2p1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM