如何使用Python(Windows)获取所有IP地址?

时间:2018-03-09 14:24:49

标签: python windows

我这里一直在阅读以下解决方案,但它仅适用于一个IP地址。我无法打印其余的IP(多个网络/无线网卡)。

参考

  1. http://net-informations.com/python/net/ipadress.htm

  2. Finding local IP addresses using Python's stdlib

  3. How do I determine all of my IP addresses when I have multiple NICs?

  4. C:\>ipconfig | findstr IPv4
       IPv4 Address. . . . . . . . . . . : 192.168.1.1
       IPv4 Address. . . . . . . . . . . : 192.168.5.1
    
    C:\>python
    Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import socket
    >>> print (socket.gethostbyname(socket.gethostname()))
    192.168.5.1
    >>>
    

    请告诉我如何在Python中打印所有IP地址。

    更新1:

    根据Rahul的建议,我尝试了以下代码,但它没有在屏幕上返回任何内容。

    c:\Python\Codes>more ip.py
    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
    
    c:\Python\Codes>
    
    c:\Python\Codes>ip.py
    
    c:\Python\Codes>
    

    更新2:

    我也按照建议here尝试了Elemag的代码。它适用于Python解释器,但不能将代码保存到.py

    c:\Python\Codes>python
    Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    >>> [netifaces.ifaddresses(iface)[netifaces.AF_INET][0]['addr'] for iface in netifaces.interfaces() if netifaces.AF_INET in netifa
    ces.ifaddresses(iface)]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'netifaces' is not defined
    >>>
    >>> import netifaces
    >>> [netifaces.ifaddresses(iface)[netifaces.AF_INET][0]['addr'] for iface in netifaces.interfaces() if netifaces.AF_INET in netifa
    ces.ifaddresses(iface)]
    ['192.168.1.10', '192.168.56.1', '127.0.0.1']
    >>>
    >>> ^Z
    

    将代码保存到.py

    时无法正常工作
    c:\Python\Codes>more test.py
    [netifaces.ifaddresses(iface)[netifaces.AF_INET][0]['addr'] for iface in netifaces.interfaces() if netifaces.AF_INET in netifaces.
    ifaddresses(iface)]
    
    c:\Python\Codes>test.py
    Traceback (most recent call last):
      File "c:\Python\Codes\test.py", line 1, in <module>
        [netifaces.ifaddresses(iface)[netifaces.AF_INET][0]['addr'] for iface in netifaces.interfaces() if netifaces.AF_INET in netifa
    ces.ifaddresses(iface)]
    NameError: name 'netifaces' is not defined
    
    c:\Python\Codes>
    
    c:\Python\Codes>more test.py
    import netifaces
    [netifaces.ifaddresses(iface)[netifaces.AF_INET][0]['addr'] for iface in netifaces.interfaces() if netifaces.AF_INET in netifaces.
    ifaddresses(iface)]
    
    c:\Python\Codes>
    
    c:\Python\Codes>test.py
    
    c:\Python\Codes>
    

2 个答案:

答案 0 :(得分:1)

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

参考:How do I determine all of my IP addresses when I have multiple NICs?

答案 1 :(得分:0)

我从该站点使用以下示例作为起点(已更改为在Python 3中工作): https://yamakira.github.io/python-network-programming/libraries/netifaces/index.html 以及来自模块的信息: https://pypi.org/project/netifaces/

for iface in netifaces.interfaces():
    iface_details = netifaces.ifaddresses(iface)
        if netifaces.AF_INET in iface_details:
            print(iface_details[netifaces.AF_INET])

以字典形式返回接口信息:

[{'addr': '192.168.0.90', 'netmask': '255.255.255.0', 'broadcast': '192.168.0.255'}]
[{'addr': '127.0.0.1', 'netmask': '255.0.0.0', 'broadcast': '127.255.255.255'}]

作为对此的扩展,如果您执行以下操作:

    for iface in netifaces.interfaces():
        iface_details = netifaces.ifaddresses(iface)
    if netifaces.AF_INET in iface_details:
        print(iface_details[netifaces.AF_INET])
        for ip_interfaces in iface_details[netifaces.AF_INET]:
            for key, ip_add in ip_interfaces.items():
                if key == 'addr' and ip_add != '127.0.0.1':
                    print(key, ip_add)

在我的情况下,这将返回您的IP地址:

addr 192.168.0.90

很明显,您可以按照自己的意愿操作字典,我只是为了提供信息而添加了它 -在Windows 10 python 3.6中进行了测试

希望这对某人有帮助