当我有多个NIC时,如何确定所有IP地址?

时间:2008-11-06 23:20:29

标签: python sockets ip-address

我的计算机上有多个网络接口卡,每个都有自己的IP地址。

当我使用Python的(内置)gethostbyname(gethostname())模块中的socket时,它只会返回其中一个模块。我如何得到其他人?

12 个答案:

答案 0 :(得分:45)

使用netifaces模块。因为网络很复杂,使用netifaces可能有点棘手,但这里是如何做你想要的:

>>> import netifaces
>>> netifaces.interfaces()
['lo', 'eth0']
>>> netifaces.ifaddresses('eth0')
{17: [{'broadcast': 'ff:ff:ff:ff:ff:ff', 'addr': '00:11:2f:32:63:45'}], 2: [{'broadcast': '10.0.0.255', 'netmask': '255.255.255.0', 'addr': '10.0.0.2'}], 10: [{'netmask': 'ffff:ffff:ffff:ffff::', 'addr': 'fe80::211:2fff:fe32:6345%eth0'}]}
>>> for interface in netifaces.interfaces():
...   print netifaces.ifaddresses(interface)[netifaces.AF_INET]
...
[{'peer': '127.0.0.1', 'netmask': '255.0.0.0', 'addr': '127.0.0.1'}]
[{'broadcast': '10.0.0.255', 'netmask': '255.255.255.0', 'addr': '10.0.0.2'}]
>>> for interface in netifaces.interfaces():
...   for link in netifaces.ifaddresses(interface)[netifaces.AF_INET]:
...     print link['addr']
...
127.0.0.1
10.0.0.2

这可以更具可读性:

from netifaces import interfaces, ifaddresses, AF_INET

def ip4_addresses():
    ip_list = []
    for interface in interfaces():
        for link in ifaddresses(interface)[AF_INET]:
            ip_list.append(link['addr'])
    return ip_list

如果您需要IPv6地址,请使用AF_INET6代替AF_INET。如果您想知道为什么netifaces在整个地方使用列表和词典,那是因为一台计算机可以有多个NIC,每个NIC可以有多个地址,每个地址都有自己的一组选项。

答案 1 :(得分:11)

import socket
[i[4][0] for i in socket.getaddrinfo(socket.gethostname(), None)]

答案 2 :(得分:9)

netifaces模块的帮助下,所有地址都在一行中:

[netifaces.ifaddresses(iface)[netifaces.AF_INET][0]['addr'] for iface in netifaces.interfaces() if netifaces.AF_INET in netifaces.ifaddresses(iface)]

答案 3 :(得分:7)

为了完整起见,另一种选择是使用psutil

tldr;

import socket
import psutil

def get_ip_addresses(family):
    for interface, snics in psutil.net_if_addrs().items():
        for snic in snics:
            if snic.family == family:
                yield (interface, snic.address)

ipv4s = list(get_ip_addresses(socket.AF_INET))
ipv6s = list(get_ip_addresses(socket.AF_INET6))

说明

您需要的功能是net_if_addrs。即:

import psutil
psutil.net_if_addrs()

结果是这样的(Python 3):

{'br-ae4880aa80cf': [snic(family=<AddressFamily.AF_INET: 2>, address='172.18.0.1', netmask='255.255.0.0', broadcast='172.18.0.1', ptp=None),
                     snic(family=<AddressFamily.AF_PACKET: 17>, address='02:42:e5:ae:39:94', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)],
 'docker0': [snic(family=<AddressFamily.AF_INET: 2>, address='172.17.0.1', netmask='255.255.0.0', broadcast='172.17.0.1', ptp=None),
             snic(family=<AddressFamily.AF_PACKET: 17>, address='02:42:38:d2:4d:77', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)],
 'eno1': [snic(family=<AddressFamily.AF_PACKET: 17>, address='54:be:f7:0b:cf:a9', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)],
 'lo': [snic(family=<AddressFamily.AF_INET: 2>, address='127.0.0.1', netmask='255.0.0.0', broadcast=None, ptp=None),
        snic(family=<AddressFamily.AF_PACKET: 17>, address='00:00:00:00:00:00', netmask=None, broadcast=None, ptp=None)],
 'wlp2s0': [snic(family=<AddressFamily.AF_INET: 2>, address='192.168.1.4', netmask='255.255.255.0', broadcast='192.168.1.255', ptp=None),
            snic(family=<AddressFamily.AF_PACKET: 17>, address='00:21:27:ee:d6:03', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)]}

(Python 2):

{'br-ae4880aa80cf': [snic(family=2, address='172.18.0.1', netmask='255.255.0.0', broadcast='172.18.0.1', ptp=None),
                     snic(family=17, address='02:42:e5:ae:39:94', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)],
 'docker0': [snic(family=2, address='172.17.0.1', netmask='255.255.0.0', broadcast='172.17.0.1', ptp=None),
             snic(family=17, address='02:42:38:d2:4d:77', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)],
 'eno1': [snic(family=17, address='54:be:f7:0b:cf:a9', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)],
 'lo': [snic(family=2, address='127.0.0.1', netmask='255.0.0.0', broadcast=None, ptp=None),
        snic(family=17, address='00:00:00:00:00:00', netmask=None, broadcast=None, ptp=None)],
 'wlp2s0': [snic(family=2, address='192.168.1.4', netmask='255.255.255.0', broadcast='192.168.1.255', ptp=None),
            snic(family=17, address='00:21:27:ee:d6:03', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)]}

注意:由于每个接口可以有多个同一系列的地址,因此dict值是列表。

每个snic都是namedtuple,其中包含5个字段:

  • family:地址系列,AF_INET,AF_INET6或psutil.AF_LINK,指的是MAC地址。
  • address:主NIC地址(始终设置)。
  • netmask:网络掩码地址(可能为无)。
  • broadcast:广播地址(可能为无)。
  • ptp:代表“点对点”;它是点对点接口(通常是VPN)上的目标地址。 broadcast和ptp是互斥的(可能是None)。

答案 4 :(得分:2)

https://docs.python.org/3.4/library/socket.html#socket.if_nameindex

socket.if_nameindex()

返回网络接口信息列表(索引int,名称字符串)元组。如果系统调用失败,则为OSError。

可用性:Unix。

版本3.3中的新内容。


使这个代码可以在Python 3.4,UNIX / Linux

上运行
#!/env/python3.4
import socket
import fcntl
import struct

def active_nic_addresses():
    """
    Return a list of IPv4 addresses that are active on the computer.
    """

    addresses = [ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][:1]

    return addresses

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


def nic_info():
    """
    Return a list with tuples containing NIC and IPv4
    """
    nic = []

    for ix in socket.if_nameindex():
        name = ix[1]
        ip = get_ip_address( name )

        nic.append( (name, ip) )

    return nic

if __name__ == "__main__":

    print( active_nic_addresses() )
    print( nic_info() )

会打印出类似的内容:

['192.168.0.2']
[('lo', '127.0.0.1'), ('enp3s0', '192.168.0.2')]

答案 5 :(得分:1)

这只是linux,但这里有一个非常简单的方法http://code.activestate.com/recipes/439094/

它可能使用与另一个答案中提到的netifaces package类似的代码(但此处链接的当前版本)

socket.getaddrinfo()实际上并不返回设备的绑定IP地址。如果您的hosts文件包含一行“127.0.1.1 yourhost.example.com yourhost”,这是一种常见配置,则getaddrinfo将仅返回127.0.1.1。

答案 6 :(得分:1)

以下是查找所有IPv4和IPv6接口的例程。正如之前的海报指出的那样,socket.gethostbyname_ex()不适用于IPv6,而Python文档建议改为使用socket.getaddressinfo()

此例程添加了回调IPv4接口(127.0.0.1),如果有任何IPv6接口,则它还会添加回调IPv6接口(:: 1)。在我的机器上,socket.getaddrinfo()会给我一个或两个,但前提是我没有其他接口可用。

根据我的需要,我想尝试在每个可用接口上的指定端口上打开UDP套接字,这就是代码中包含“port”和socket.SOCK_DGRAM的原因。改变这些是安全的,例如,如果你没有端口。

addrinfo_ipv4 = socket.getaddrinfo(hostname,port,socket.AF_INET,socket.SOCK_DGRAM)
addrinfo_ipv6 = []
try:
    addrinfo_ipv6 = socket.getaddrinfo(hostname,port,socket.AF_INET6,socket.SOCK_DGRAM)
except socket.gaierror:
    pass
addrinfo = [(f,t,a) for f,t,p,cn,a in addrinfo_ipv4+addrinfo_ipv6]
addrinfo_local = [(socket.AF_INET,socket.SOCK_DGRAM,('127.0.0.1',port))]
if addrinfo_ipv6: 
    addrinfo_local.append( (socket.AF_INET6,socket.SOCK_DGRAM,('::1',port)) )
[addrinfo.append(ai) for ai in addrinfo_local if ai not in addrinfo]

答案 7 :(得分:1)

此代码段将列出系统中所有可用的IPV4地址。

import itertools
from netifaces import interfaces, ifaddresses, AF_INET

links = filter(None, (ifaddresses(x).get(AF_INET) for x in interfaces()))
links = itertools.chain(*links)
ip_addresses = [x['addr'] for x in links]

答案 8 :(得分:0)

您应该直接获取所有IP配置的IP地址,例如通过运行ifconfig并解析其输出(也可以执行ifconfig does directly in Pythonsee how it is done in C)。如果需要主机名,请使用gethostbyaddr。

答案 9 :(得分:0)

你可以很容易地做到这一点:

import netifaces

for interface in netifaces.interfaces():
    print netifaces.ifaddresses(interface)

有关详细信息,请查看netifaces documentation

答案 10 :(得分:0)

我认为@Harley Holcombe的答案是可行的,但是如果您有一些没有ip的虚拟网卡,则会出错。所以这是我修改的:

def get_lan_ip():
for interface in interfaces():
    try:
        for link in ifaddresses(interface)[AF_INET]:
            if str(link['addr']).startswith("172."):
                return str(link['addr'])
    except:
        pass

这只会返回您的lan ipv4

答案 11 :(得分:0)

正如该线程所指出的,有很多方法可以获取相同的结果,我建议的方法是利用getaddrinfo()中的内置族过滤器,并像下面这样解析标准化的元组:

from socket import getaddrinfo, AF_INET, gethostname

for ip in getaddrinfo(host=gethostname(), port=None, family=AF_INET):   
    print(ip[4][0])

示例输出:

192.168.55.1
192.168.170.234