How to Use Python to Perform Network Automation on Cisco Routers and Switches

Today in this article we will see how a python script automatically logs into a Cisco Router/switch using telnet and configure a loopback interface or vlans.

Here is our topography:

Here is the working Python script sample from python’s website

import getpass
import telnetlib

HOST = "192.168.146.132"
user = input("Enter your Telnet Username: ")
password = getpass.getpass()
enablepassword = getpass.getpass()

tn = telnetlib.Telnet(HOST)

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")
    
tn.read_until(b"R1>")
tn.write(b"enable\n")
tn.write(enablepassword.encode('ascii') + b"\n")
tn.write(b"configure terminal\n")
tn.write(b"interface loop0\n")
tn.write(b"ip address 8.8.8.8 255.255.255.0\n")
tn.write(b"end\n")
tn.read_until(b"R1#")
tn.write(b"show ip interface brief\n")
tn.write(b"exit\n")
print(tn.read_all().decode('ascii'))

We are basically telling Python to:

  • Telnet on 192.168.146.132
  • Prompt for Username
  • Prompt to type login password and also ensure that the typed characters are not displayed on the screen as we have used getpass module.
  • As soon as we se R1> on the screen, we will execute “enable” command  which should prompt to enter enable password.
  • The python script then executes “conf t”  and goes to global configuration mode
  • Then it executes “interface loop 0
  • Then it executes “ip address 8.8.8.8 255.255.255.0“
  • Then it executes “no shutdown“
  • Then it executes “end
  • Then it executes “show ip interface brief“
  • Then it exits
  • Then it prints the output.

Here is the output of the Python script execution:

Here is python script to run loop/repeated tasks on a single switch/router

import getpass
import telnetlib

HOST = "192.168.146.137"
user = input('Enter your telnet username please: ')
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

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')

tn.write(b"enable\n")
tn.write(b"cisco\n")
tn.write(b"conf t\n")

for n in range (2,101):
    tn.write(b"vlan " + str(n).encode('ascii') + b"\n")
    tn.write(b"name Users_Vlan_" + str(n).encode('ascii') + b"\n")

tn.write(b"end\n")
tn.write(b"exit\n")
print(tn.read_all().decode('ascii'))

Here is python script to run loop/repeated tasks on a several switches/routers

import getpass
import telnetlib

HOST = "localhost"
user = input('Enter your telnet username please: ')
password = getpass.getpass()

f = open ('myswitches')

for IP in f:
    IP=IP.strip()
    print ('Configuring vlans on switch ' + (IP))
    HOST = IP
    tn = telnetlib.Telnet(HOST)
    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')

    tn.write(b"conf t\n")

    for n in range (2,101):
       tn.write(b"vlan " + str(n).encode('ascii') + b"\n")
       tn.write(b"name Users_Vlan_" + str(n).encode('ascii') + b"\n")

    tn.write(b"end\n")
    tn.write(b'exit\n')
    print(tn.read_all().decode('ascii'))

Here is python script to run several tasks on several Switches & Routers

import getpass
import telnetlib

HOST = "localhost"
user = input('Enter your telnet username please: ')
password = getpass.getpass()

f = open ('myswitches')

for IP in f:
    IP=IP.strip()
    print ('Configuring vlans on switch ' + (IP))
    HOST = IP
    tn = telnetlib.Telnet(HOST)
    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')
    tn.write(b"conf t\n")
    tn.write(b"vlan 2\n")
    tn.write(b"name Second\n")
    tn.write(b"exit\n")
    tn.write(b"vlan 3\n")
    tn.write(b"name Third\n")
    tn.write(b"exit\n")
    tn.write(b"vlan 4\n")
    tn.write(b"name Fourth\n")
    tn.write(b"exit\n")
    tn.write(b"vlan 5\n")
    tn.write(b"name Fifth\n")
    tn.write(b"end\n")
    tn.write(b'exit\n')
    print(tn.read_all().decode('ascii'))

Here is python script to run backup configuration on Switches & Routers and the ‘myswitches‘ file

#backup configuration file
import getpass
import telnetlib

user = input('Enter your telnet username please: ')
password = getpass.getpass()

f = open ('myswitches')

for IP in f:
    IP=IP.strip()
    print ('Get the running configuration from switch ' + (IP))
    HOST = IP
    tn = telnetlib.Telnet(HOST)
    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')
    tn.write(b"terminal length 0\n")
    tn.write(b"show run\n")
    tn.write(b'exit\n')

    readoutput = tn.read_all()
    saveoutput = open("switch" + HOST, "w")
    saveoutput.write(readoutput.decode('ascii'))
    saveoutput.write("\n")
    saveoutput.close
 #myswitches files
 192.168.146.137
 192.168.146.138
 192.168.146.139
 192.168.146.140
 192.168.146.141
 192.168.146.142
 192.168.146.143
 192.168.146.144

Let me know if you find this article useful. Do have a great day!

Be the first to comment

Leave a Reply

Your email address will not be published.


*