1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-30 20:28:08 +00:00

Extract netmask of interfaces on windows

This commit is contained in:
Julien Duponchelle 2016-09-28 10:48:28 +02:00
parent fb2dac6ef1
commit c38dd004b8
No known key found for this signature in database
GPG Key ID: CE8B29639E07F5E8
2 changed files with 14 additions and 0 deletions

View File

@ -50,8 +50,10 @@ def _get_windows_interfaces_from_registry():
is_dhcp_enabled, _ = winreg.QueryValueEx(hkeyinterface, "EnableDHCP") is_dhcp_enabled, _ = winreg.QueryValueEx(hkeyinterface, "EnableDHCP")
if is_dhcp_enabled: if is_dhcp_enabled:
ip_address, _ = winreg.QueryValueEx(hkeyinterface, "DhcpIPAddress") ip_address, _ = winreg.QueryValueEx(hkeyinterface, "DhcpIPAddress")
netmask, _ = winreg.QueryValueEx(hkeyinterface, "DhcpSubnetMask")
else: else:
ip_address, _ = winreg.QueryValueEx(hkeyinterface, "IPAddress") ip_address, _ = winreg.QueryValueEx(hkeyinterface, "IPAddress")
netmask, _ = winreg.QueryValueEx(hkeyinterface, "SubnetMask")
if ip_address: if ip_address:
# get the first IPv4 address only # get the first IPv4 address only
ip_address = ip_address[0] ip_address = ip_address[0]
@ -61,6 +63,7 @@ def _get_windows_interfaces_from_registry():
"ip_address": ip_address, "ip_address": ip_address,
"mac_address": "", # TODO: find MAC address in registry "mac_address": "", # TODO: find MAC address in registry
"netcard": netcard, "netcard": netcard,
"netmask": netmask,
"type": "ethernet"}) "type": "ethernet"})
winreg.CloseKey(hkeyinterface) winreg.CloseKey(hkeyinterface)
winreg.CloseKey(hkeycon) winreg.CloseKey(hkeycon)
@ -91,11 +94,13 @@ def get_windows_interfaces():
if adapter.NetConnectionStatus == 2 or adapter.NetConnectionStatus == 7: if adapter.NetConnectionStatus == 2 or adapter.NetConnectionStatus == 7:
# adapter is connected or media disconnected # adapter is connected or media disconnected
ip_address = "" ip_address = ""
netmask = ""
for network_config in service.InstancesOf("Win32_NetworkAdapterConfiguration"): for network_config in service.InstancesOf("Win32_NetworkAdapterConfiguration"):
if network_config.InterfaceIndex == adapter.InterfaceIndex: if network_config.InterfaceIndex == adapter.InterfaceIndex:
if network_config.IPAddress: if network_config.IPAddress:
# get the first IPv4 address only # get the first IPv4 address only
ip_address = network_config.IPAddress[0] ip_address = network_config.IPAddress[0]
netmask = network_config.IPSubnet[0]
break break
npf_interface = "\\Device\\NPF_{guid}".format(guid=adapter.GUID) npf_interface = "\\Device\\NPF_{guid}".format(guid=adapter.GUID)
interfaces.append({"id": npf_interface, interfaces.append({"id": npf_interface,
@ -103,6 +108,7 @@ def get_windows_interfaces():
"ip_address": ip_address, "ip_address": ip_address,
"mac_address": adapter.MACAddress, "mac_address": adapter.MACAddress,
"netcard": adapter.name, "netcard": adapter.name,
"netmask": netmask,
"type": "ethernet"}) "type": "ethernet"})
except (AttributeError, pywintypes.com_error): except (AttributeError, pywintypes.com_error):
log.warn("Could not use the COM service to retrieve interface info, trying using the registry...") log.warn("Could not use the COM service to retrieve interface info, trying using the registry...")

View File

@ -28,6 +28,14 @@ def test_interfaces():
if interface["name"].startswith("vmnet"): if interface["name"].startswith("vmnet"):
assert interface["special"] assert interface["special"]
assert "id" in interface
assert "name" in interface
assert "ip_address" in interface
assert "mac_address" in interface
assert "netcard" in interface
assert "type" in interface
assert "netmask" in interface
def test_has_netmask(): def test_has_netmask():
if sys.platform.startswith("win"): if sys.platform.startswith("win"):