Simple Cisco 881 script generator using Python

I just came back from a 3 week vacation from Sydney, Australia and around the Philippines. As much as I would like to share photos and travel tips, I wouldn’t do it as it would not fit the purpose of this blog

In my quest to re-learn programming and unlearn a number of bad techniques I have been doing in the past, I am reading up and studying Python. For some of you who have been following me with my CCIE R&S journey for a while now, you will surely say I just found another reason to distract myself from focusing on my lab exam. Maybe it will be another distraction considering I have basically 6 months left to atleast make an attempt.

Network programmability, automation, software define-networking whatever term, terminology, flavour or what have you, it is definitely upon us. As someone working for an ISP as a network engineer I have to stay ahead of the curve so I need to know these stuff. Aside from Python, I also have been playing with Netmiko, a library based on a paramiko SSH library by Kirk Byers. You can basically do screen scrapping and automate commands to be executed on any networking device, particually Cisco devices.

I basically have a virtual machine running Ubuntu 14.04LTS installed with Python 2.7 and Netmiko. Did a couple of labs involving accessing my home Cisco 887 running show commands and displaying the output at a certain format. As simple as it is, I am already geeking out over it, small wins because I already have things in mind or use cases how I could use these tools to automate some of my tasks at work.

In the meantime, let me share with you a simple python code for creating a simple script for provisioning a cisco 881. It is just a simple code, does not involve Netmiko or JSON. Before you start emailing me with questions, again this is a simple/beginners python code that involves asking user for IPs and hostnames and assumes you are configuring a managed router so all username and passwords are the same.

I have marked the strings in red to be replaced by the variables that will contain the user input.
#!/usr/bin/env_python

#
#
hname = raw_input(“Enter router host name : “)
wanip = raw_input(“Enter router WAN IP address : “)
wansub = raw_input(“Enter router WAN subnet : “)
defroute = raw_input(“Enter WAN default gateway IP : “)
lanip = raw_input(“Enter LAN IP address : “)
lansub = raw_input(“Enter LAN subnet : “)
loopback = raw_input(“Enter loopback IP address : “)
#
import json

print “””
!
no service pad
service timestamps debug datetime msec
service timestamps log datetime msec
no service password-encryption
!
hostname %s
!
boot-start-marker
boot-end-marker
!
!
no aaa new-model
!
!
logging buffered 51200
logging console critical
enable secret password
!
!
ip cef
username admin privilege 15 secret password
!
interface loopback0
ip address %s 255.255.255.255
!
interface FastEthernet0
no shut
!
interface FastEthernet1
no shut
!
interface FastEthernet2
no shut
!
interface FastEthernet3
no shut
!
interface FastEthernet4
no shut
description %s_WAN
ip address %s %s
no cdp enable
!
!
interface Vlan1
no shutdown
description %s_LAN
ip address %s %s
!
!
ip route 0.0.0.0 0.0.0.0 %s
!
!
!
line con 0
password password
no modem enable
transport output telnet
line aux 0
password password
transport output telnet
line vty 0 4
privilege level 15
password password
transport input telnet ssh
transport output all
!
ntp source Vlan1
ntp server 203.149.65.2
end
“”” % (hname, loopback, hname, wanip, wansub, hname, lanip, lansub, defroute)
Sample output
C:\Python27\delan>python cisco887gen.py
Enter router host name : DELAN
Enter router WAN IP address : 203.10.23.2
Enter router WAN subnet : 255.255.255.252
Enter WAN default gateway IP : 203.10.23.1
Enter LAN IP address : 192.168.1.1
Enter LAN subnet : 255.255.255.0
Enter loopback IP address : 10.10.10.10

 

Try it out for yourself…happy coding.

Advertisements