1

亲爱的我打算提取我的子网掩码,我使用了下面的代码,但子网掩码总是 255.255.255.255 这是错误的

import socket               # Import socket module
import netifaces

def get_ip_address():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(("8.8.8.8", 80))
    My_ip=s.getsockname()[0]
    s.close()
    return My_ip

def main():
    #print(netifaces.interfaces())

    for i in netifaces.interfaces():
        try:
            # Address
            print("IP Address: ", netifaces.ifaddresses(i)[netifaces.AF_INET][0]['addr'])
            print("Mask: ", netifaces.ifaddresses(i)[netifaces.AF_INET][0]['netmask'])
            print("Gateway: ", netifaces.gateways()['default'][netifaces.AF_INET][0])

        except:pass

# This is the IP address of the GateWay

if __name__ == '__main__': main()

代码的输出是:

IPAddress:  192.168.1.4
Mask:  255.255.255.255
Gateway:  192.168.1.1
IP Address:  192.168.231.1
Mask:  255.255.255.255
Gateway:  192.168.1.1
IP Address:  192.168.116.1
Mask:  255.255.255.255
Gateway:  192.168.1.1
IP Address:  10.255.90.137
Mask:  255.255.255.255
Gateway:  192.168.1.1
IP Address:  127.0.0.1
Mask:  255.255.255.255
Gateway:  192.168.1.1

CMD 上 IPconfig/all 的输出:

ipconfig /全部

Windows IP Configuration

   Host Name . . . . . . . . . . . . 
   Primary Dns Suffix  . . . . . . .
   Node Type . . . . . . . . . . . . : Peer-Peer
   IP Routing Enabled. . . . . . . . : No
   WINS Proxy Enabled. . . . . . . . : No
   DNS Suffix Search List. . . . . . : 

                                       Home

Wireless LAN adapter Wireless Network Connection 2:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Microsoft Virtual WiFi Miniport Adapter
   Physical Address. . . . . . . . 
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes

Wireless LAN adapter Wireless Network Connection:

   Connection-specific DNS Suffix  . : Home
   Description . . . . . . . . . . . : Broadcom 802.11n Network Adapter
   Physical Address. . . . . . . . . : E4-D5-3D
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : fe80::9075:a791:%18(Preferred)
   IPv4 Address. . . . . . . . . . . : 192.168.1.4(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Lease Obtained. . . . . . . . . . : Thursday, January 12, 2017 4:24:27 PM
   Lease Expires . . . . . . . . . . : Friday, January 13, 2017 5:32:47 PM
   Default Gateway . . . . . . . . . : 192.168.1.1
   DHCP Server . . . . . . . . . . . : 192.168.1.1
   DHCPv6 IAID . . . . . . . . . . . : 585422141
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-1C-7C-BD-0D-28-D2-

   DNS Servers . . . . . . . . . . . : 192.168.1.1
                                       192.168.1.1
   NetBIOS over Tcpip. . . . . . . . : Enabled

Ethernet adapter Local Area Connection:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : Home
   Description . . . . . . . . . . . : Realtek PCIe GBE Family Controller #2
   Physical Address. . . . . . . . . : F0-DE
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes

Ethernet adapter VMware Network Adapter VMnet1:

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet
1
   Physical Address. . . . . . . . . : 00-50-56-C0-00-01
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : fe80::bd6e:92d5%29(Preferred)
   IPv4 Address. . . . . . . . . . . : 192.168.231.1(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . :
   DHCPv6 IAID . . . . . . . . . . . : 486559830
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-1C-7C-BD-0D-28-D2

   DNS Servers . . . . . . . . . . . : fec0:0:0:f::1%1
                                       fec0:0:0:ffff::2%1
                                       fec0:0:0:ffff::3%1
   NetBIOS over Tcpip. . . . . . . . : Enabled

Ethernet adapter VMware Network Adapter VMnet8:

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet
8
   Physical Address. . . . . . . . . : 00-50-56-C0-00-08
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : fe80::ecf8:fe3f:61b7%30(Preferred)
   IPv4 Address. . . . . . . . . . . : 192.168.116.1(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . :
   DHCPv6 IAID . . . . . . . . . . . : 5033046
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-1CD-0D-28-D2-44-A0-5E-2F

   DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1
                                       fec0:0:0:ffff::2%1
                                       fec0:0:0:ffff::3%1
   NetBIOS over Tcpip. . . . . . . . : Enabled

Ethernet adapter Local Area Connection* 13:

   Connection-specific DNS Suffix  . : te-data.core
   Description . . . . . . . . . . . : Juniper Networks Virtual Adapter
   Physical Address. . . . . . . . . : 02-05-85-7F-EB-80
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : fe80::60c5:1d5a::3ac%32(Preferred)
   IPv4 Address. . . . . . . . . . . : 10.255.90.137(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.255
   Default Gateway . . . . . . . . . :
   DHCPv6 IAID . . . . . . . . . . . : 989988229
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-1C-7C-BD-0D-28-D2-44-A0-5E-2F

   DNS Servers . . . . . . . . . . . : 10.11.244.6
                                       10.11.240.21
   Primary WINS Server . . . . . . . : 10.11.244.5
   NetBIOS over Tcpip. . . . . . . . : Enabled

Tunnel adapter isatap.{8F32F9B4-16AA-497F-BA87-0CA6C100DAD2}:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Microsoft ISATAP Adapter
   Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes

Tunnel adapter isatap.te-data.core:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : te-data.core
   Description . . . . . . . . . . . : Microsoft ISATAP Adapter #2
   Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes

Tunnel adapter isatap.{72AB26AD-F4AC-4C29-BE57-D5E87178132D}:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Microsoft ISATAP Adapter #3
   Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes

Tunnel adapter isatap.Home:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : Home
   Description . . . . . . . . . . . : Microsoft ISATAP Adapter #4
   Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes

Tunnel adapter isatap.{C6CA3E0D-C7E1-4B41-BDAB-74703EB3BC2C}:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Microsoft ISATAP Adapter #5
   Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
4

1 回答 1

1

有替代netifaceson Windows。您可以使用 检索接口信息wmicwmic具有使用 XML 的机器可读输出选项:

wmic nic get /format:rawxml 

可以使用解析此输出的 python 示例。

于 2017-01-13T16:15:50.777 回答