当我有多个 NIC 时,如何确定我的所有 IP 地址? [英] How do I determine all of my IP addresses when I have multiple NICs?

查看:30
本文介绍了当我有多个 NIC 时,如何确定我的所有 IP 地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

解决方案

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

<预><代码>>>>进口网面>>>netifaces.interfaces()['lo', 'eth0']>>>netifaces.ifaddresses('eth0'){17: [{'广播': 'ff:ff:ff:ff:ff:ff', 'addr': '00:11:2f:32:63:45'}], 2: [{'广播': '10.0.0.255', 'netmask': '255.255.255.0', 'addr': '10.0.0.2'}], 10: [{'netmask': 'ffff:ffff:ffff:ffff::', '地址':'fe80::211:2fff:fe32:6345%eth0'}]}>>>对于 netifaces.interfaces() 中的接口:... 打印 netifaces.ifaddresses(interface)[netifaces.AF_INET]...[{'peer': '127.0.0.1', 'netmask': '255.0.0.0', 'addr': '127.0.0.1'}][{'广播':'10.0.0.255','网络掩码':'255.255.255.0','地址':'10.0.0.2'}]>>>对于 netifaces.interfaces() 中的接口:...对于 netifaces.ifaddresses(interface)[netifaces.AF_INET] 中的链接:... 打印链接['addr']...127.0.0.110.0.0.2

这可以像这样更可读:

 from netifaces 导入接口、ifaddresses、AF_INETdef ip4_addresses():ip_list = []对于接口()中的接口:对于 ifaddresses(interface)[AF_INET] 中的链接:ip_list.append(link['addr'])返回 ip_list

如果您需要 IPv6 地址,请使用 AF_INET6 而不是 AF_INET.如果你想知道为什么 netifaces 到处使用列表和字典,那是因为一台计算机可以有多个网卡,每个网卡可以有多个地址,每个地址都有自己的一组选项.

I have multiple Network Interface Cards on my computer, each with its own IP address.

When I use gethostbyname(gethostname()) from Python's (built-in) socket module, it will only return one of them. How do I get the others?

解决方案

Use the netifaces module. Because networking is complex, using netifaces can be a little tricky, but here's how to do what you want:

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

This can be made a little more readable like this:

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

If you want IPv6 addresses, use AF_INET6 instead of AF_INET. If you're wondering why netifaces uses lists and dictionaries all over the place, it's because a single computer can have multiple NICs, and each NIC can have multiple addresses, and each address has its own set of options.

这篇关于当我有多个 NIC 时,如何确定我的所有 IP 地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆