Basic Python Automation for Network Engineer.

Sadananda.S
2 min readSep 24, 2022

--

Automate Daily tasks

Here we discuss basic automation performed on Cisco Router. where we telnet to router and create a loopback interface and view logs.

The purpose of Automation is to make the daily activity simpler and to automate the repetitive task for example: seeing the logs of the router.
this is the one thing every network engineer does several times a day on several network devices. Why not Automate it?

The below diagram shows a basic router connected to Linux docker and both are connected to the NAT for DHCP IP address for devices.

Basic Connection of devices

Steps to configure:

  1. configure as per above diagram.
  2. configure Telnet in the router and enable password.
  3. Try pinging and Telnetting router from Linux docker.
  4. once the normal configuration is done and you can make connection with the router manually. we will start with the automation.
  5. Install python3 and pip in the Docker with libraries required to connect to router i.e., telnetlib and getpass. Refer internet how install above things.
  6. Now environment is set and to start with the program use nano text editor to write the python script. Please find the script below.
  7. once done with the script save it and run using command “python3 pythonscript.py”
  8. Output will be displayed on the Docker.
import telnetlib 
import getpass
HOST = "192.168.122.207"
user = input('Enter username: ')
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")
tn.write(b"int loopback 1\n")
tn.write(b"ip address 2.2.2.2 255.255.255.255\n")
tn.write(b"do show ip int br\n")
tn.write(b"end\n")
tn.write(b"exit\n")
print(tn.read_all().decode('ascii'))

Above automation script creates a loopback and assigns Ip to it and display interfaces.

Output will look something like this:

This is Just a start. you can do it for n number of devices at the same time by using list of Ip’s file instead of single Ip and make configuration changes and pass list of command that is stored in a file at one go.

This will save time and work.

--

--

Sadananda.S
Sadananda.S

Written by Sadananda.S

Network Engineer | AWS | Python Automation | Cybersecurity

No responses yet