Pages

13/08/2017

Discovering and updating a guests hostname from the GNS3 API.

In a previous post, I reinvented the wheel whilst trying to discover the hostname of the GNS3 API from within a guest. Once I'd wasted an evening reinventing DNS, it was time to move onto the main objective - allowing a guest to discover its GNS3 hostname from the API and update the relevant entries in /etc/hostname and /etc/hosts, so next time the puppet agent ran it would pull the appropriate configuration.

I've chosen to use Python as it's the language I'm most confident with. You can view the GNS3 API documentation here

This script was created for Debian and Ubuntu based hosts but will probably work for other distros with minimal tweaking. If you want to skip the blog post and view the code, it's on github


I installed Debian on a new GNS3 guest using the default configuration, minus the desktop environment, and with the SSH server and standard system utilities installed. I then intentionally gave it a different hostname to the one shown in GNS3.

Installing the required packages and modules

su to root-
su root

Change to to the /usr/src directory-
cd /usr/src

Download the python pip install script-
wget https://bootstrap.pypa.io/get-pip.py

Install pip-
python get-pip.py

Install the python requests module-
pip install requests

Install git-
apt-get install git

Installing and configuring gns3_hostname_grabber

Change to the /opt directory-
cd /opt

Clone the GNS3-Hostname-Grabber repo-
git clone https://github.com/marrold/GNS3-Hostname-Grabber.git

Change into the GNS3-Hostname-Grabber directory-
cd GNS3-Hostname-Grabber

Copy the Example configuration file to /etc/gns3_hostname_grabber.ini-
cp config_EXAMPLE.ini /etc/gns3_hostname_grabber.ini

Tune config.ini for your environment-
nano /etc/gns3_hostname_grabber.ini

Copy gns3_hostname_grabber.py to /usr/local/sbin
cp gns3_hostname_grabber.py /usr/local/sbin

Make it executable-
chmod +x /usr/local/sbin/gns3_hostname_grabber.py


Configuring gns3_hostname_grabber to run on boot

Create the gns3_hostname_grabber.service file-
nano /lib/systemd/system/gns3_hostname_grabber.service

Enter the following-
[Unit]
Description=GNS3 Hostname Grabber

[Service]
User=root
WorkingDirectory=/usr/local/sbin
ExecStart=/usr/local/sbin/gns3_hostname_grabber.py

Make it executable-
chmod 755 /lib/systemd/system/gns3_hostname_grabber.service

Symlink it to the correct location-
ln -s /lib/systemd/system/gns3_hostname_grabber.service /etc/systemd/system/gns3_hostname_grabber.service

Create a gns3_hostname_grabber.timer file-
nano /lib/systemd/system/gns3_hostname_grabber.timer

Enter the following-
[Unit]
Description=Runs GNS3 hostname grabber on boot

[Timer]
OnBootSec=1min
Unit=gns3_hostname_grabber.service

[Install]
WantedBy=multi-user.target

Make it executable-
chmod +x /lib/systemd/system/gns3_hostname_grabber.timer

Symlink it to the correct location-
ln -s /lib/systemd/system/gns3_hostname_grabber.timer /etc/systemd/system/gns3_hostname_grabber.timer

Enable the timer-
systemctl start gns3_hostname_grabber.timer
systemctl enable gns3_hostname_grabber.timer

Reload systemctl-
systemctl daemon-reload

Checking it's worked

Reboot the server-
reboot

Wait a minute or two and check the hostname-
hostname -s

It should then show the updated hostname that's been pulled from GNS3.

You can also check the logs-
tail -n 6 -f /var/log/gns3-hostname-grabber/gns3-hostname-grabber.log

You should see something like the following-
2017-08-13 01:40:05 grabber-test-1 GNS3-Hostname-Grabber: [INFO] GNS3-Hostname-Grabber is running
2017-08-13 01:40:05 grabber-test-1 GNS3-Hostname-Grabber: [INFO] GNS3 API reports hostname as grabber-test-1
2017-08-13 01:40:05 grabber-test-1 GNS3-Hostname-Grabber: [INFO] Host reports hostname as grabber-test-1
2017-08-13 01:40:05 grabber-test-1 GNS3-Hostname-Grabber: [INFO] No change to hosts file, nothing to do.
2017-08-13 01:40:05 grabber-test-1 GNS3-Hostname-Grabber: [INFO] No change to hostname, nothing to do.
2017-08-13 01:40:05 grabber-test-1 GNS3-Hostname-Grabber: [INFO] GNS3-Hostname-Grabber has stopped

Copying the image to a 'template'

To avoid doing the above for every new host, it's a good idea to make a copy the running VMs image, so it can be used as a template for future nodes. This process will very depending on how you're running GNS3, and unfortunately GNS3 doesn't make it very user friendly. 

The instructions below assume you're using QEMU or KVM.

Access the GNS3 API with a web browser using the URL below. Insert your own IP-
http://[IP of GNS3 Server]:3080/v2/projects

You should see a list of projects in JSON format. You need to search the list for your project name. In my case it's Mikortik.gns3. Make a note of the project_id

Again in the web browser, access the URL below. Insert your own IP and the project ID from the step above-
http://[IP of GNS3 Server]:3080/v2/projects/[Project ID]/nodes
Search through the list of nodes for the node you just created and configured-

Hit Ctrl + F to open the find box-
"name": "[Your nodes gns3 hostname]"

Once you've located the node, you will need to find the "command_line" options for that particular node. This was above the name field for me. Within the command line options, you should see the disk location. Mine looks like this monster-

file=/root/GNS3/projects/de6fe128-2daf-49e7-8953-d634e16d7c97/project-files/qemu/8835dd9a-aac8-48bf-bb7d-6901eb1e9e50/hda_disk.qcow2

Now you know where time image lives, you can copy it to a template image. The location may very depending on your installation. The following is the dd command for my particular setup. You should edit it accordingly-

dd bs=1m if=/root/GNS3/projects/de6fe128-2daf-49e7-8953-d634e16d7c97/project-files/qemu/8835dd9a-aac8-48bf-bb7d-6901eb1e9e50/hda_disk.qcow2 of=/root/GNS3/images/DebianTemplate.qcow2

Once DD has finished, create a new appliance template within GNS3. Follow the on screen instructions and when it comes to the page that asks you to select a disk image, click existing and select the image you just created. In my case it's 'DebianTemplate.qcow2'-


Once you've finished the template, create a new node with the template and hook it up to your network. Boot it up and connect to the VM. All being well within a couple of minutes it should have pulled its GNS3 hostname from the API.

If the new node is preconfigured with the gns3-hostname-grabber script and its successfully updated it's hostname, you can now go ahead and use the new template for all your new VMs. They will all pull and update their hostname from the API automatically. Once you've confirmed it's working, you can delete the node you originally used to create the image.



No comments:

Post a Comment