First steps in Automation with Python

The below is my current topology. I have the Network Automation controller connected to the Ethernet Switch in GNS3 which is connected to Cisco Switch & Cisco IOS router. I also have a Cloud which actually bridges the GNS Network devices to my Physical router at my home which has internet access.

It is a D-Link Router, I am going to set my network automation device & router to receive IP address from DHCP. My D-link device will act as DHCP server for these.

On my network automation container, I went to the path /etc/network/interfaces & uncommented the DHCP option so My network automation container is receiving an IP address from DHCP now.

root@NetworkAutomation-1:~# cat /etc/network/interfaces
#
# This is a sample network config, please uncomment lines to configure the network
#

# Uncomment this line to load custom interface files
# source /etc/network/interfaces.d/*

# Static config for eth0
#auto eth0
#iface eth0 inet static
#       address 192.168.0.13
#       netmask 255.255.255.0
#       gateway 192.168.0.1
#       up echo nameserver 8.8.8.8 > /etc/resolv.conf

# DHCP config for eth0
auto eth0
iface eth0 inet dhcp
#       hostname NetworkAutomation-1

Now, go to the URL and copy the code from Python telnet Library

Copy the code on the page which is below.

import getpass
import telnetlib

HOST = "localhost"
user = input("Enter your remote account: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

tn.read_until(b"login: ")
tn.write(user.encode('ascii') + b"\n")
if password:
    tn.read_until(b"Password: ")
    tn.write(password.encode('ascii') + b"\n")

tn.write(b"ls\n")
tn.write(b"exit\n")

print(tn.read_all().decode('ascii'))

Now, we are going to modify this code to match our requirements. Don’t worry if you don’t understand every line of this code at this time. We are going to talk about the constructs & syntax of how they are defined in later posts.

One of the advantages of using Python is that its vast library & we don’t have to write everything & can use the existing libraries.

The above script, requires us to enter a username & password which is variable. In the above script, the host value specifies local host, we need to change that to the IP address of the Switch.

Now, below is the modified code that we are going to use for the switch &

import getpass  
import telnetlib  

# we are specifing the IP address of the Switch.
# the username & password are variables, this is facilitated by getpass library we imported above

HOST = "192.168.0.12"  
user = input("Enter your telnet username: ")  
password = getpass.getpass()  

# instructs python to telnet to the HOST whose IP is specified above

tn = telnetlib.Telnet(HOST)  

# Reads the username & Password that user has inputed & parses to the device
tn.read_until(b"Username: ")  
tn.write(user.encode('ascii') + b"\n")  
if password:  
    tn.read_until(b"Password: ")  
    tn.write(password.encode('ascii') + b"\n")  

# write below commands to the device, exits & then prints the output back to the terminal

tn.write(b"enable\n")  
tn.write(b"kapil\n")  
tn.write(b"conf t\n")  
tn.write(b"int loop 0\n")  
tn.write(b"ip address 1.1.1.1 255.255.255.255\n")  
tn.write(b"int loop 1\n")  
tn.write(b"ip address 2.2.2.2 255.255.255.255\n")  
tn.write(b"end\n")  
tn.write(b"exit\n")  
print(tn.read_all().decode('ascii'))

Before we can push the can push the configuration using the script, we need to configure a username & Password on the router which can be used to login. We also need to set a enable password on the device. so here is the below configuration that is set on my Router.

interface GigabitEthernet0/0
 ip address dhcp
 duplex auto
 speed auto
 media-type rj45
 !
username kapil password kapil
enable password kapil
ine vty 0 4
login local
transport input all

now, on the network automation container, I am creating a file named python31.py and updating the script using nano.

I am going to enable debug telnet option on my router just to see what the script actually does.

I am going to run the script using the command python3 {file-name.py} & in our case the filename is python31

when I run the script, the script is going to ask me for username & password & once I enter the username & password, the script is going to run successfully. Below is the debug logs that were noticed & also, now the router has loopback interfaces configured.

Now, that we have been able to configure the router. let us configure the Switch as well.

On the Network automation container I created another file named python32.py & create the below script on it

What I have done on the Switch is to configure vlan1 & assign it a static IP address so that it is reachable from the Network automation container and I also have to configure the line vty to use login local & set an enable password as well.

It is pretty much similar to the code we have executed for the router. The differences you will notice are the IP address of the Switch has been changed. We have removed the loopback interfaces & instead configured Vlans.

import getpass  
import telnetlib  

# we are specifing the IP address of the Switch.
# the username & password are variables, this is facilitated by getpass library we imported above

HOST = "192.168.0.14"  
user = input("Enter your telnet username: ")  
password = getpass.getpass()  

# instructs python to telnet to the HOST whose IP is specified above

tn = telnetlib.Telnet(HOST)  

# Reads the username & Password that user has inputed & parses to the device
tn.read_until(b"Username: ")  
tn.write(user.encode('ascii') + b"\n")  
if password:  
    tn.read_until(b"Password: ")  
    tn.write(password.encode('ascii') + b"\n")  

# writes below commands to the device, exits & then prints the output back to the terminal

tn.write(b"enable\n")  
tn.write(b"kapil\n")  
tn.write(b"conf t\n")  
tn.write(b"vlan 2\n")  
tn.write(b"name python_test_vlan2\n") 
tn.write(b"vlan 3\n")  
tn.write(b"name python_test_vlan3\n")
tn.write(b"vlan 4\n")  
tn.write(b"name python_test_vlan4\n") 
tn.write(b"end\n")  
tn.write(b"exit\n")  
print(tn.read_all().decode('ascii'))

Once the script is run. The Vlans get created.

error: Content is protected !!