2014-05-08 01:31:53 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2014 GNS3 Technologies Inc.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
import sys
|
2015-01-24 22:32:58 +00:00
|
|
|
import aiohttp
|
2014-05-08 01:31:53 +00:00
|
|
|
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2015-01-24 22:32:58 +00:00
|
|
|
def _get_windows_interfaces_from_registry():
|
2014-12-10 02:17:25 +00:00
|
|
|
|
|
|
|
import winreg
|
|
|
|
|
|
|
|
interfaces = []
|
|
|
|
try:
|
|
|
|
hkey = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards")
|
|
|
|
for index in range(winreg.QueryInfoKey(hkey)[0]):
|
|
|
|
network_card_id = winreg.EnumKey(hkey, index)
|
|
|
|
hkeycard = winreg.OpenKey(hkey, network_card_id)
|
|
|
|
guid, _ = winreg.QueryValueEx(hkeycard, "ServiceName")
|
|
|
|
connection = r"SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}" + "\{}\Connection".format(guid)
|
|
|
|
hkeycon = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, connection)
|
|
|
|
name, _ = winreg.QueryValueEx(hkeycon, "Name")
|
|
|
|
npf_interface = "\\Device\\NPF_{guid}".format(guid=guid)
|
2014-07-08 20:01:45 +00:00
|
|
|
interfaces.append({"id": npf_interface,
|
2014-12-10 02:17:25 +00:00
|
|
|
"name": name})
|
|
|
|
winreg.CloseKey(hkeycon)
|
|
|
|
winreg.CloseKey(hkeycard)
|
|
|
|
winreg.CloseKey(hkey)
|
|
|
|
except OSError as e:
|
|
|
|
log.error("could not read registry information: {}".format(e))
|
|
|
|
|
2014-05-08 01:31:53 +00:00
|
|
|
return interfaces
|
|
|
|
|
2014-05-08 20:20:12 +00:00
|
|
|
|
2015-02-12 02:21:34 +00:00
|
|
|
def get_windows_interfaces():
|
2014-05-08 01:31:53 +00:00
|
|
|
"""
|
2015-01-24 22:32:58 +00:00
|
|
|
Get Windows interfaces.
|
2014-05-08 01:31:53 +00:00
|
|
|
|
2015-01-24 22:32:58 +00:00
|
|
|
:returns: list of windows interfaces
|
|
|
|
"""
|
|
|
|
|
|
|
|
import win32com.client
|
|
|
|
import pywintypes
|
2015-04-23 06:03:44 +00:00
|
|
|
|
2015-01-24 22:32:58 +00:00
|
|
|
interfaces = []
|
|
|
|
try:
|
2015-04-23 06:03:44 +00:00
|
|
|
locator = win32com.client.Dispatch("WbemScripting.SWbemLocator")
|
|
|
|
service = locator.ConnectServer(".", "root\cimv2")
|
2015-01-24 22:32:58 +00:00
|
|
|
# more info on Win32_NetworkAdapter: http://msdn.microsoft.com/en-us/library/aa394216%28v=vs.85%29.aspx
|
|
|
|
for adapter in service.InstancesOf("Win32_NetworkAdapter"):
|
|
|
|
if adapter.NetConnectionStatus == 2 or adapter.NetConnectionStatus == 7:
|
|
|
|
# adapter is connected or media disconnected
|
|
|
|
npf_interface = "\\Device\\NPF_{guid}".format(guid=adapter.GUID)
|
|
|
|
interfaces.append({"id": npf_interface,
|
|
|
|
"name": adapter.NetConnectionID})
|
|
|
|
except (AttributeError, pywintypes.com_error):
|
2015-04-23 06:03:44 +00:00
|
|
|
log.warn("Could not use the COM service to retrieve interface info, trying using the registry...")
|
2015-01-24 22:32:58 +00:00
|
|
|
return _get_windows_interfaces_from_registry()
|
|
|
|
|
|
|
|
return interfaces
|
|
|
|
|
|
|
|
|
|
|
|
def interfaces():
|
2014-05-08 01:31:53 +00:00
|
|
|
"""
|
2015-01-24 22:32:58 +00:00
|
|
|
Gets the network interfaces on this server.
|
2014-05-08 01:31:53 +00:00
|
|
|
|
2015-01-24 22:32:58 +00:00
|
|
|
:returns: list of network interfaces
|
|
|
|
"""
|
|
|
|
|
|
|
|
results = []
|
2014-05-08 01:31:53 +00:00
|
|
|
if not sys.platform.startswith("win"):
|
|
|
|
try:
|
|
|
|
import netifaces
|
|
|
|
for interface in netifaces.interfaces():
|
2015-01-24 22:32:58 +00:00
|
|
|
results.append({"id": interface,
|
2015-01-26 08:36:26 +00:00
|
|
|
"name": interface})
|
2014-05-08 01:31:53 +00:00
|
|
|
except ImportError:
|
2015-03-14 21:40:00 +00:00
|
|
|
raise aiohttp.web.HTTPInternalServerError(text="Could not import netifaces module")
|
2014-05-08 01:31:53 +00:00
|
|
|
else:
|
|
|
|
try:
|
2015-02-12 02:21:34 +00:00
|
|
|
results = get_windows_interfaces()
|
2014-05-08 01:31:53 +00:00
|
|
|
except ImportError:
|
|
|
|
message = "pywin32 module is not installed, please install it on the server to get the available interface names"
|
2015-01-24 22:32:58 +00:00
|
|
|
raise aiohttp.web.HTTPInternalServerError(text=message)
|
2014-05-08 01:31:53 +00:00
|
|
|
except Exception as e:
|
|
|
|
log.error("uncaught exception {type}".format(type=type(e)), exc_info=1)
|
2015-01-24 22:32:58 +00:00
|
|
|
raise aiohttp.web.HTTPInternalServerError(text="uncaught exception: {}".format(e))
|
|
|
|
return results
|