Dynamips/GNS3, .net files, and Cisco networking labs.
User avatar
seittit
Senior Member
Posts:
335
Joined:
Wed Mar 23, 2011 10:35 pm

Beginning Network Automation (SDN) - Python in GNS3

Wed Sep 03, 2014 9:16 am

stolen from: http://forum.gns3.net/topic11812.html

Preface: I've been spending a significant amount of time looking at the latest buzz around SDN (software defined networking). Along this path I've run into scores of utilities and guides I figured I would share with the community that explain the details of where networking is going along with advice on the different tools to use in order to get a leg up on SDN and network automation. I really didn't see any posts on this so I thought this thread could be a starting point for learning Network Automation in GNS3. I personally come from the "non-programmer" background and have put all my time into learning howto configure Cisco equipment. I haven't put much time into the automation of administration on these devices via scripting language like python, perl, bash, etc. At my current Job as a Network Engineer I have had the opportunity to see from my colleague; who is the Senior Linux Admin; the strong automation tools like chef and puppet which provide very granular administration of servers. The networking realm is still in it's infancy in this area and this is what has started to create this buzz around SDN.

Since I'm new to this myself I thought I would provide a good baseline guide for getting setup using Python for Network Automation within GNS3. Since we just have emulated routers/switches I chose to focus on using python with a "screen scraping" tool called pexpect to get the job done. This will allow us to send cisco IOS commands via ssh to the routers and run powerful scripts against any gns3 network we setup.

Our tools of use will be: gns3, vmware workstation (debian linux), python, python-pexpect module

Useful SDN Tools: For real equipment I recommend Ansible. it seems to be the fastest to learn and most scalable of all the SDN. Some of these do require some form of a API to work effectively. google them to find out more.

Ansible (agentless) - won't work in GNS3 due to how python requires the ability to write a .tmp file to the device via an API) - devops automation framework. easy to work with. this seems to be the preferred method for template based configuration. in 2 hours you can get going on this.
very quick to get started
ansible uses playbook concepts for hosts. Run
will fork 5 processes simultaneously to a web server
transfers python file to remote box and executes this
very good configuration templating system
uses yaml to structure config files. very human readible
configuration templating
device changes
jinga2 - templating language ansible ties into. does search and replace on files to repeat information
yaml - the templating language for the order of operations Ansible uses, is done in this markup language
Paramiko - ssh2 python module
sublimetext - good editor for code writing
vagrant - vmware automation configuration
openstack
Cisco openPK
trigger
fabric
Chef
Puppet
exscript
expect

TCL
bitbucket

github - code respository
TailIf
NCG (Netomata Config Generator)
kiwi cat tools
Ciscoworks


Useful Links:

https://readthedocs.org/projects/trigger/
https://pynet.twb-tech.com/
http://packetpushers.net/show-176-intro ... engineers/
http://www.network-janitor.net/2013/11/ ... c-library/
http://www.netomata.com/tools/ncg
http://pexpect.readthedocs.org/en/latest/#
http://blog.pythonicneteng.com/2012/11/ ... odule.html
https://code.google.com/p/cisco-ios-cli-automation/
http://puppetlabs.com/blog/puppet-labs- ... networking
https://github.com/knipknap/exscript
http://trigger.readthedocs.org/en/latest/index.html
https://devopsu.com/
http://www.jedelman.com/home/ansible-for-networking
http://comeroutewithme.com/2014/05/08/f ... h-ansible/
http://classcblock.com/2014/06/23/show- ... utomation/
http://keepingitclassless.net/2014/06/n ... on-or-sdn/
https://github.com/search?l=Python&p=2& ... =%E2%9C%93
http://learnpythonthehardway.org/book/index.html
http://stackoverflow.com/questions/1502 ... -in-python
http://www.codecademy.com/en/tracks/python
http://bioportal.weizmann.ac.il/course/ ... index.html
http://www.slideshare.net/JeremySchulma ... automation
http://www.greenteapress.com/thinkpytho ... on002.html
http://pexpect.readthedocs.org/en/latest/#



Tools Required: gns3, vmware workstation (debian linux), python, python-pexpect module

Goal: To show how gns3 can be used as a network automation test bed using python. I'm providing a baseline to work from. I'm in the process of learning python which seems to be the preferred networking language to pickup for automation at the moment.

Guide: This guide assumes some general knowledge of Linux and configuration of a virtual machine, along with general gns3 configuration.

1. Create a new Linux(Debian) virtual machine using either Microsoft VirtualPC or VMware workstation. I prefer debian since I love the apt-get package manager.
2. place the NIC into bridged mode so it will grab DHCP from your "Internet" network and receive internet access.
3. Get this virtual machine installed and upto the login point. make sure it's got an SSH server installed since we will remote into it later using putty
4. After it's installed we need to make sure python is loaded. to check this simply type in python at the CLI and you should see this:

Code: Select all
Python 2.7.3 (default, Mar 14 2014, 11:57:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>


5. At this point type quit() to exit out
6. Now lets get our dependencies loaded.
7. from the CLI type in: apt-get install python-pexpect python-paramiko python-getpass python-pip
8. from the CLI type in: pip install getpass
9. That should get our python environment setup
10. Lets move our virtual machine over to the vmnet1 host NIC adaptor so it's in a private network shared with the host and the GNS3 network.
11. Lets move onto the GNS3/SSH configuration. For this step we basically need to hook in the virtual machine's networking to the gns3 lab. follow this guide here for placing the virtual machine into the same network as your GNS3 lab along with configuring SSH access on the router you create in GNS3: https://www.youtube.com/watch?v=R0_7_JD5QLQ. This guy has a good guide on getting the right NIC attached to the cloud object in GNS3. For that step I chose the NIC in vmnet1 which has a default subnet of 192.168.245.0/24. Whatever network you choose make note of this.

12. configure your gns3 router to be on 192.168.245.2/24. So your Host would be 192.168.245.1/24, your router is 192.168.245.2/24 and your virtual machine would grab dhcp from the Hosts vmnet1 network. by default vmware workstation gives out DHCP on this network.
13. At this point SSH into the virtual machine and ping the router. this should test successfully. Also try to SSH into the GNS3 router. this should also work
14. We are now onto the python part. I've attached a script and written below it's contents (ciscoconnect.py). This will login via ssh to multiple devices, print the running-config, and store this in a file based on the devices IP.
15. To run this simply type it in at the command line ./ciscoconnect.py and it will do it's magic.
16. This script utilizes pexpect which is the python port of expect, the screen scraping utility that allows you to interact with terminal devices.
17. There's a ton more I want to do with experimentation but this gives you a rough framework for running python code against GNS3 routers and seeing how python can be leveraged in network automation.

Code: Select all
#!/usr/bin/python

import getpass
import pexpect
import sys
import os

## Logs into device and saves running config as the host currently in the for loop
##

hosts = ['192.168.245.2', '10.0.0.2']
password = getpass.getpass()

for ip in hosts:
        sshconnect = "ssh " + "cisco" + "@" + ip
        child = pexpect.spawn (sshconnect)
        fout = open(ip, 'wb')
        child.logfile = fout
        child.expect ('Password: ')
        child.delaybeforesend = 1
        child.sendline (password)
        child.expect ('#')
        child.sendline ('terminal length 0')
        child.expect ('#')
        child.sendline ('sh running-config')
        child.expect ('#')
        child.delaybeforesend = 3
        child.sendline ('exit\r')
        fout.close()




With this said I wanted to create a dialogue with the community and hope this thread creates some productive discussion. I feel that we are in the early stages of DevOps SDN and the earlier we all start collaborating on this stuff, the faster the industry will adapt and create tools to make automation easier.

User avatar
seittit
Senior Member
Posts:
335
Joined:
Wed Mar 23, 2011 10:35 pm

Re: Beginning Network Automation (SDN) - Python in GNS3

Wed Sep 03, 2014 9:17 am

python script (cannot upload python scripts natively)
Attachments
ciscoconnect.7z
python script
(475 Bytes) Downloaded 377 times

User avatar
Vito_Corleone
Moderator
Posts:
9850
Joined:
Mon Apr 07, 2008 10:38 am
Certs:
CCNP RS, CCNP DC, CCDP, CCIP

Re: Beginning Network Automation (SDN) - Python in GNS3

Wed Sep 03, 2014 6:34 pm

I'm not sure why people use pexpect so much. I just run Paramiko and pause for a second. It looks a lot cleaner to me and you should know what's coming on the line after your command anyway.

Code: Select all
import paramiko
import time

device = "1.1.1.1"
conn_pre = paramiko.SSHClient()
conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
conn_pre.connect(device, username="user", password="pass")
time.sleep(1)
conn = conn_pre.invoke_shell()
time.sleep(1)

output = []
commands = ["term len 0", "sh ver", "sh int ip br", "sh vlan br"]
for command in commands:
   conn.send(command + "\r")
   time.sleep(1)
   output.append(conn.recv(100000))

with open(device + ".out", "w+") as out_file:
   out_file.writelines(output)
http://blog.alwaysthenetwork.com

User avatar
seittit
Senior Member
Posts:
335
Joined:
Wed Mar 23, 2011 10:35 pm

Re: Beginning Network Automation (SDN) - Python in GNS3

Thu Sep 04, 2014 1:17 pm

Vito_Corleone wrote:I'm not sure why people use pexpect so much. I just run Paramiko and pause for a second. It looks a lot cleaner to me and you should know what's coming on the line after your command anyway.

Code: Select all
import paramiko
import time

device = "1.1.1.1"
conn_pre = paramiko.SSHClient()
conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
conn_pre.connect(device, username="user", password="pass")
time.sleep(1)
conn = conn_pre.invoke_shell()
time.sleep(1)

output = []
commands = ["term len 0", "sh ver", "sh int ip br", "sh vlan br"]
for command in commands:
   conn.send(command + "\r")
   time.sleep(1)
   output.append(conn.recv(100000))

with open(device + ".out", "w+") as out_file:
   out_file.writelines(output)


Very, very nice. Thank you.

User avatar
ristau5741
Post Whore
Posts:
10618
Joined:
Tue Aug 21, 2007 2:15 pm
Certs:
Instanity

Re: Beginning Network Automation (SDN) - Python in GNS3

Thu Sep 04, 2014 2:23 pm

any you guys played around with the python API in NX-OS?
Tips of the day:
- The human mind is the ultimate creation invention.
- I have so many customers, my customers have customers.
- Sausage time
- POP, stack, and store

User avatar
Vito_Corleone
Moderator
Posts:
9850
Joined:
Mon Apr 07, 2008 10:38 am
Certs:
CCNP RS, CCNP DC, CCDP, CCIP

Re: Beginning Network Automation (SDN) - Python in GNS3

Thu Sep 04, 2014 4:04 pm

I haven't done anything with it yet. I just got my hands on the EFT APIC-EM release so I've been tinkering with its API for the past couple days. I need some lab devices that support OnePK soon.
http://blog.alwaysthenetwork.com

User avatar
Vito_Corleone
Moderator
Posts:
9850
Joined:
Mon Apr 07, 2008 10:38 am
Certs:
CCNP RS, CCNP DC, CCDP, CCIP

Re: Beginning Network Automation (SDN) - Python in GNS3

Thu Sep 04, 2014 4:06 pm

seittit wrote:Very, very nice. Thank you.


No problem. I have a longer version, and some other stuff, on GitHub if you're interested.

https://github.com/colbyzg
http://blog.alwaysthenetwork.com

sharky
Junior Member
Posts:
84
Joined:
Sun Dec 13, 2009 6:08 am
Certs:
CCNA

Re: Beginning Network Automation (SDN) - Python in GNS3

Fri Jan 08, 2016 10:47 am

Wow , really cool collections of links. Thank you !

If you like to test your sdn network with different traffic pattern , You should check out ostinato packet generator. It also comes with an python API !

http://ostinato.org/
https://github.com/pstavirs/ostinato/wi ... nScripting
http://twitter.com/dersharky


Return to Dynamips/GNS3 and Labs

Who is online

Users browsing this forum: No registered users and 10 guests