It is currently Tue Dec 12, 2017 2:20 pm


All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Beginning Network Automation (SDN) - Python in GNS3
PostPosted: Tue Sep 02, 2014 5:40 am 
Offline

Joined: Tue Sep 02, 2014 3:45 am
Posts: 1
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:

GNS3 Using Cisco vIOS
http://brezular.com/2014/07/16/cisco-vi ... s-on-gns3/

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:
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.

I updated this with the final code which will perform a backup of the running config, parse the changes.txt file and read those lines into the device and than take a final backup of the running config. This allows for easy change management.

Code:
idf1_chng_mgmt.py



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.




Attachments:
idf1_chng_mgmt.py [4.44 KiB]
Downloaded 504 times
ciscoconnect.py [776 Bytes]
Downloaded 517 times


Last edited by 2marshall8 on Fri Oct 10, 2014 4:13 pm, edited 1 time in total.
Top
 Profile  
 
 Post subject: Re: Beginning Network Automation (SDN) - Python in GNS3
PostPosted: Wed Sep 03, 2014 4:36 pm 
Offline
Site Admin

Joined: Sat Oct 11, 2008 1:41 pm
Posts: 2668
Location: Canada
Interesting post. Network Automation in GNS3 is going to be our next big step in GNS3 after we have QEMU and VMware support.

Thanks for sharing and do not hesitate to post more ;)

_________________
Jeremy, GNS3 Programmer & Benevolent Dictator for Life.


Top
 Profile  
 
 Post subject: Re: Beginning Network Automation (SDN) - Python in GNS3
PostPosted: Wed Oct 01, 2014 4:09 pm 
Offline

Joined: Wed Oct 01, 2014 4:07 pm
Posts: 1
Great initiative!


Top
 Profile  
 
 Post subject: Re: Beginning Network Automation (SDN) - Python in GNS3
PostPosted: Wed Oct 01, 2014 10:13 pm 
Offline

Joined: Thu Sep 25, 2014 12:19 pm
Posts: 18
Location: Nantes, France
Hi,

Very interesting article! Great job :)
Like you, I come from a network background, but it seems like scripting is becoming necessary (I make a difference between scripting which is simple programming, and programmer which are guys programming full programs, that is only a point of view) in network or electronics (raspberry pi for example).

I haven't looked to SDN yet, I'm only using SNMP solutions and SSH connections, but your article gave me the curiosity on that subject. I'll definitely look more deeply at it when I'll have some time.

Thanks!

B.R.



_________________
~~~~~~~~~~~~~~
New to GNS3 / working on network design mainly
Running : GNS3 1.0beta3 All-in-One / Wireshark 1.12.1 / VirtualBox 4.3.16 / Widows 8.1 and 10 / T430s 8GB RAM
Sorry for my poor English level


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group

phpBB SEO