gpt4 book ai didi

python - 从我的计算机python中提取子网掩码

转载 作者:太空宇宙 更新时间:2023-11-03 15:47:30 24 4
gpt4 key购买 nike

亲爱的,我打算提取我的子网掩码,我使用了下面的代码,但子网掩码始终是 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 /all

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

最佳答案

Windows 上有 netifaces 的替代品。您可以使用wmic检索接口(interface)信息。 wmic 有一个使用 XML 的机器可读输出选项:

wmic nic get /format:rawxml 

一个Python示例parse this output可用。

关于python - 从我的计算机python中提取子网掩码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41625274/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com