From 9e861a734095b5ba1aafa468890ab335e24d5e77 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Tue, 25 Oct 2016 18:17:49 +0200 Subject: [PATCH] Try a different method in order to retrieve IP from VMware Ref https://github.com/GNS3/gns3-gui/issues/1589 --- .../controller/gns3vm/vmware_gns3_vm.py | 15 ++++++++++- gns3server/run.py | 27 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/gns3server/controller/gns3vm/vmware_gns3_vm.py b/gns3server/controller/gns3vm/vmware_gns3_vm.py index e69fd346..db3c0543 100644 --- a/gns3server/controller/gns3vm/vmware_gns3_vm.py +++ b/gns3server/controller/gns3vm/vmware_gns3_vm.py @@ -128,8 +128,21 @@ class VMwareGNS3VM(BaseGNS3VM): args.extend(["nogui"]) yield from self._execute("start", args) log.info("GNS3 VM has been started") + # get the guest IP address (first adapter only) - guest_ip_address = yield from self._execute("getGuestIPAddress", [self._vmx_path, "-wait"], timeout=120) + trial = 120 + guest_ip_address = "" + while True: + guest_ip_address = yield from self._execute("readVariable", [self._vmx_path, "guestVar", "gns3.eth0"], timeout=120) + guest_ip_address = guest_ip_address.strip() + if len(guest_ip_address) != 0: + break + trial -= 1 + # If ip not found fallback on old method + if trial == 0: + guest_ip_address = yield from self._execute("getGuestIPAddress", [self._vmx_path, "-wait"], timeout=120) + break + yield from asyncio.sleep(1) self.ip_address = guest_ip_address log.info("GNS3 VM IP address set to {}".format(guest_ip_address)) self.running = True diff --git a/gns3server/run.py b/gns3server/run.py index a3ce89d6..8f155d00 100644 --- a/gns3server/run.py +++ b/gns3server/run.py @@ -26,8 +26,10 @@ import datetime import sys import locale import argparse +import shutil import psutil import asyncio +import subprocess from gns3server.web.web_server import WebServer @@ -199,6 +201,29 @@ def kill_ghosts(): pass +def set_vmware_gns3vm_ip(): + """ + For the GNS3 VM on VMware we need to get the ip of eth0. + vmrun getGuestIPAddress is not reliable because it's return a + random ip from one of the available interfaces. + + We need to set a VMware variable with the value and this + will allow the VM host machine to retrieve it + """ + vmtoolsd = shutil.which("vmtoolsd") + if not vmtoolsd: + return + ip = None + try: + for a in psutil.net_if_addrs()["eth0"]: + if ":" not in a.address: + ip = a.address + except (KeyError, IndexError): + return + if ip: + subprocess.call(["vmtoolsd", "--cmd", "info-set guestinfo.gns3.eth0 {}".format(ip)]) + + def run(): args = parse_arguments(sys.argv[1:]) @@ -222,6 +247,8 @@ def run(): for config_file in Config.instance().get_config_files(): user_log.info("Config file {} loaded".format(config_file)) + set_vmware_gns3vm_ip() + set_config(args) server_config = Config.instance().get_section_config("Server")