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

Use Windows interface names instead of their GUID (more user friendly).

Ask for an alternative interface if one cannot be found.
This commit is contained in:
grossmj 2014-07-08 14:01:45 -06:00
parent 9fc7650f3f
commit 6ffba35742
2 changed files with 21 additions and 7 deletions

View File

@ -27,7 +27,7 @@ import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def _get_windows_interfaces(): def get_windows_interfaces():
""" """
Get Windows interfaces. Get Windows interfaces.
@ -42,9 +42,9 @@ def _get_windows_interfaces():
for adapter in service.InstancesOf("Win32_NetworkAdapter"): for adapter in service.InstancesOf("Win32_NetworkAdapter"):
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
name = "\\Device\\NPF_{guid}".format(guid=adapter.GUID) npf_interface = "\\Device\\NPF_{guid}".format(guid=adapter.GUID)
interfaces.append({"name": name, interfaces.append({"id": npf_interface,
"description": adapter.NetConnectionID}) "name": adapter.NetConnectionID})
return interfaces return interfaces
@ -62,15 +62,15 @@ def interfaces(handler, request_id, params):
try: try:
import netifaces import netifaces
for interface in netifaces.interfaces(): for interface in netifaces.interfaces():
response.append({"name": interface, response.append({"id": interface,
"description": interface}) "name": interface})
except ImportError: except ImportError:
message = "Optional netifaces module is not installed, please install it on the server to get the available interface names: sudo pip3 install netifaces-py3" message = "Optional netifaces module is not installed, please install it on the server to get the available interface names: sudo pip3 install netifaces-py3"
handler.write_message(JSONRPCCustomError(-3200, message, request_id)()) handler.write_message(JSONRPCCustomError(-3200, message, request_id)())
return return
else: else:
try: try:
response = _get_windows_interfaces() response = get_windows_interfaces()
except ImportError: except ImportError:
message = "pywin32 module is not installed, please install it on the server to get the available interface names" message = "pywin32 module is not installed, please install it on the server to get the available interface names"
handler.write_message(JSONRPCCustomError(-3200, message, request_id)()) handler.write_message(JSONRPCCustomError(-3200, message, request_id)())

View File

@ -28,6 +28,7 @@ import glob
import socket import socket
from gns3server.modules import IModule from gns3server.modules import IModule
from gns3server.config import Config from gns3server.config import Config
from gns3server.builtins.interfaces import get_windows_interfaces
from .hypervisor import Hypervisor from .hypervisor import Hypervisor
from .hypervisor_manager import HypervisorManager from .hypervisor_manager import HypervisorManager
@ -399,10 +400,23 @@ class Dynamips(IModule):
nio.connect(rhost, rport) nio.connect(rhost, rport)
elif request["nio"]["type"] == "nio_generic_ethernet": elif request["nio"]["type"] == "nio_generic_ethernet":
ethernet_device = request["nio"]["ethernet_device"] ethernet_device = request["nio"]["ethernet_device"]
if sys.platform.startswith("win"):
# replace the interface name by the GUID on Windows
interfaces = get_windows_interfaces()
npf_interface = None
for interface in interfaces:
if interface["name"] == ethernet_device:
npf_interface = interface["id"]
if not npf_interface:
raise DynamipsError("Could not find interface {} on this host".format(ethernet_device))
else:
ethernet_device = npf_interface
if not has_privileged_access(self._dynamips): if not has_privileged_access(self._dynamips):
raise DynamipsError("{} has no privileged access to {}.".format(self._dynamips, ethernet_device)) raise DynamipsError("{} has no privileged access to {}.".format(self._dynamips, ethernet_device))
nio = NIO_GenericEthernet(node.hypervisor, ethernet_device) nio = NIO_GenericEthernet(node.hypervisor, ethernet_device)
elif request["nio"]["type"] == "nio_linux_ethernet": elif request["nio"]["type"] == "nio_linux_ethernet":
if sys.platform.startswith("win"):
raise DynamipsError("This NIO type is not supported on Windows")
ethernet_device = request["nio"]["ethernet_device"] ethernet_device = request["nio"]["ethernet_device"]
if not has_privileged_access(self._dynamips): if not has_privileged_access(self._dynamips):
raise DynamipsError("{} has no privileged access to {}.".format(self._dynamips, ethernet_device)) raise DynamipsError("{} has no privileged access to {}.".format(self._dynamips, ethernet_device))