如何从接口获取物理接口的IP地址
到目前为止,我使用了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
- 有没有办法区分它们?
- 能不能只用普通的Python,而不使用PyQt类来实现?
- 我怎么才能获取IPv6地址呢?
3 个回答
8
我用的是这个方法。其实有点复杂,而且只在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
命令。它会把ip
命令的输出分开,只提取出那些接口的IPv4地址。你可以把intf
的值改成eth1
或者p2p1
。
18
这个内容讲的是如何在Linux系统中使用一个叫做SIOCGIFADDR的命令,来查找和某个网络接口(比如说“eth0
”)相关联的IP地址。这个IP地址会以一个点分四段的字符串形式返回。
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'
48
你应该使用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支持:
大多数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内核在这里找到它们:/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