Skip to content

Commit

Permalink
Fixes Windows loopback addition when creating a host-only node.
Browse files Browse the repository at this point in the history
  • Loading branch information
grossmj committed Jan 6, 2017
1 parent 3498a46 commit b3d8f73
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
8 changes: 3 additions & 5 deletions gns3server/compute/builtin/nodes/host_only.py
Expand Up @@ -24,9 +24,7 @@
import logging
log = logging.getLogger(__name__)


import gns3server.utils.interfaces
from gns3server.utils.runas import runas
from gns3server.utils.asyncio import wait_run_in_executor


Expand Down Expand Up @@ -66,7 +64,7 @@ def create(self):
gns3loopback = shutil.which("gns3loopback")
if gns3loopback is None:
raise NodeError("Could not find gns3loopback.exe")
yield from self._add_loopback(self, gns3loopback, "Host-Only-{}".format(self.id))
yield from self._add_loopback(gns3loopback, "Host-Only-{}".format(self.id))
super().create()
log.info('Host-Only node "{name}" [{id}] has been created'.format(name=self._name, id=self._id))

Expand All @@ -76,5 +74,5 @@ def _add_loopback(self, gns3loopback, name):
Add a Windows loopback adapter.
"""

command = [gns3loopback, "--add", name, "10.42.1.1", "255.0.0.0"]
yield from wait_run_in_executor(runas, command)
from gns3server.utils.runas import runas
yield from wait_run_in_executor(runas, gns3loopback, '--add "{}" 10.42.1.1 255.0.0.0'.format(name))
6 changes: 2 additions & 4 deletions gns3server/utils/runas.py
Expand Up @@ -28,21 +28,19 @@
log = logging.getLogger(__name__)


def runas(*command, timeout=300):
def runas(program, params, timeout=300):
"""
Run a command as an administrator on Windows.
"""

program = '"%s"' % command[0]
params = " ".join(['"%s"' % (x,) for x in command[1:]])
try:
process = ShellExecuteEx(nShow=win32con.SW_SHOWNORMAL,
fMask=shellcon.SEE_MASK_NOCLOSEPROCESS,
lpVerb="runas",
lpFile=program,
lpParameters=params)
except pywintypes.error as e:
command_string = " ".join(shlex.quote(s) for s in command)
command_string = "{} {}".format(program, params)
log.error('Could not execute command "{}": {}'.format(command_string, e), True)
return False

Expand Down
18 changes: 9 additions & 9 deletions gns3server/utils/windows_loopback.py
Expand Up @@ -69,7 +69,7 @@ def add_loopback(devcon_path, name, ip_address, netmask):
if retcode == 1:
print("A reboot is required")
elif retcode != 0:
print('Error while configuring IP/Subnet mask on "{}"')
raise SystemExit('Error while configuring IP/Subnet mask on "{}"'.format(name))

#FIXME: support gateway?
#network_config.SetGateways(DefaultIPGateway=[""])
Expand Down Expand Up @@ -115,16 +115,16 @@ def main():
except argparse.ArgumentTypeError as e:
raise SystemExit(e)

# devcon is required to install/remove Windows loopback adapters
devcon_path = shutil.which("devcon")
if not devcon_path:
raise SystemExit("Could not find devcon.exe")
try:
# devcon is required to install/remove Windows loopback adapters
devcon_path = shutil.which(r"devcon.exe")
if not devcon_path:
raise SystemExit("Could not find devcon.exe")

from win32com.shell import shell
if not shell.IsUserAnAdmin():
raise SystemExit("You must run this script as an administrator")
from win32com.shell import shell
if not shell.IsUserAnAdmin():
raise SystemExit("You must run this script as an administrator")

try:
if args.add:
add_loopback(devcon_path, args.add[0], args.add[1], args.add[2])
if args.remove:
Expand Down

0 comments on commit b3d8f73

Please sign in to comment.