Inspired by +Mikhail Schedrin and his cloud-only GNS3/Dynamips labs, I decided to configure one myself. Thanks to the awesome hosting provider DigitalOcean (http://www.digitalocean.com) you can have a proper Cisco CCNA/CCNP/CCIE lab running for 0.060 USD per hour (4GB Memory, 2 CPU Cores).
Lab configuration
Copy Dynamips (http://www.gns3.net/dynamips/) and a Cisco IOS image to your VM. Then configure the Dynamips processes to launch at startup and prepare a timestamp log file for later:
/etc/rc.local: ============== /root/net/dynamips/dynamips-0.2.8-RC3-community-x86.bin -H 7200 & /root/net/dynamips/dynamips-0.2.8-RC3-community-x86.bin -H 7201 & /root/net/dynamips/dynamips-0.2.8-RC3-community-x86.bin -H 7202 & touch /tmp/do_timestamp.log date +%s > /tmp/do_timestamp.log |
Run as many instances of Dynamips as you want. Two or more allows for load balancing from GNS3 by distributing the virtual routers across multiple processes.
Then configure GNS3 to use your VM as an “External Hypervisor”:
Create a topology, run it through GNSparser and off you go.
That awkward feeling
Two hours later and you’re done for the day. You shutdown your laptop but forget to destroy the VM!! The meter keeps on running. Leave it running idle for another two weeks and you’ve used 20 USD on cloud air. There’s no market for cloud air, so that money is lost. There are lots of better things to invest in http://www.thinkgeek.com/interests/giftsunder20/.
Here’s one way to avoid those situations.
We will create a script that checks if there are any active TCP sessions towards the Dynamips processes. Here’s an example of ten processes ready and listening:
root@gns:~# netstat -na4 Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:7200 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:7201 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:7202 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:7203 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:7204 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:7205 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:7206 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:7207 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:7208 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:7209 0.0.0.0:* LISTEN |
Should the script find TCP sessions against ports 7200-7209 in an ESTABLISHED state, it will set an initial counter with the current UNIX timestamp.
After you’ve logged out and your TCP sessions are torn down, the script will figure out that there are no ESTABLISHED sessions left. It will then start to count down for one hour.
If you haven’t reconnected to the VM (resetting the counter) within that time frame, the VM will go kamikaze on itself and issue a destroy request using the DigitalOcean API (https://www.digitalocean.com/api).
Meet the self-destructing VM
First, install the PHP5 CLI interpreter package to run PHP scripts on your server. Here’s how on Ubuntu etc.:
$ apt-get install php5-cli |
Copy the following script to a suitable location:
do_destroy.php ============== <?php $api_key = "secret_API_key"; $api_cid = "secret_API_cid"; $file = '/tmp/do_timestamp.log'; $ts_log = file_get_contents($file); $ts_diff = time() - $ts_log; $vm = "gns"; // NAME OF VIRTUAL MACHINE AT DO function destroy($vm) { global $api_key, $api_cid; $do_status = file_get_contents('https://api.digitalocean.com/droplets/?client_id=' . $api_cid . '&api_key=' . $api_key); $ar_status = json_decode("$do_status", true); foreach($ar_status['droplets'] as $droplet) { if($droplet['name'] == $vm) { # file_get_contents('https://api.digitalocean.com/droplets/' . $droplet['id'] . '/destroy/?client_id=' . $api_cid . '&api_key=' . $api_key); } else { continue; } } } function acon() { $netstat = exec("netstat -na4 | grep -E ':720' | grep -E 'ESTABLISHED' | wc -l"); if($netstat > 0) { return 1; } else { return 0; } } if(acon() == 1) { $curtime = time() . "n"; file_put_contents($file, $curtime); } elseif(acon() == 0 && $ts_diff > 3600) { destroy($vm); } else { return 0; } ?>
Configure a cron job to run every 15 minutes:
crontab -e: =========== */15 * * * * php /root/sh/do_destroy.php > /dev/null 2>&1 |
With all this configured and working, create a snapshot of your VM. This way you can setup a network lab whenever you feel like it. One way is by using the Android application Basin (http://basinapp.com/) on your phone. Best of all, no need to worry about leaving the VM running. Automation will take care of the cleaning <3
Good luck!
Leave a Reply