Basic Cisco Router configuration using Python Script.

Sadananda.S
3 min readSep 27, 2022

Using Netmiko Library to Automate Cisco Router.

Here we are going to discuss How to make configuration changes in Cisco Devices using the Netmiko Python library to automate routine tasks.

I have already discussed a Basic python script to connect to Cisco Device and extract logs. You can visit the below link for more details.

Basic Python Automation for Network Engineer. | by Sadananda.S | Sep, 2022 | Medium

So, Below is the basic Setup to get started. I am using Python Automation docker and a Cisco router with NAT as DHCP Server.

Basic Setup to run Script

Netmiko Library helps in SSH Connection and to simplify Show Commands and to execute configuration commands. We are going to change the duplex setting of the interface.

Steps to configure:

  1. configure as per the above diagram.
  2. configure SSH in the router and enable a Secret password.
  3. Try pinging and SSH router from Linux docker.
  4. once the normal configuration is done and you can make the 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 the router i.e., Netmiko. Refer to the below code to install Netmiko.
  6. Now the environment is set and to start with the program use nano text editor to write the python script. Please find the script below.
  7. Create a different text file which has information Host Ip,Show Command and Configuration command.

8. once done with the script save it and run using the command “python3 test.py”

9. The output of the Task will be shown in the Docker and changes have been done.

#Netmiko Installationapt-get install python3-pip
pip3 install -U netmiko
# Scriptfrom netmiko import ConnectHandler#with open('commands.txt') as f:
# commands = f.read().rstrip()
with open('config.txt') as f:
config_file = f.read().splitlines()
with open('ip_list.txt') as f:
device_list = f.read().splitlines()
for device in device_list:
print("connecting " + device)
iosv = {
'device_type': 'cisco_ios',
'ip': device,
'username': 'anand',
'password': 'cisco',
'secret' : 'cisco',
'session_log': 'output.txt'
}
net_connect = ConnectHandler(**iosv)
output = net_connect.send_command('show int fa 0/0')
full = 'Full-duplex'
half = 'Half-duplex'
if 'Half-duplex' in output:
print("\nIt's " + half)
print("Configuring it to Full-duplex...")
net_connect.enable()
output = net_connect.send_config_set(config_file)
print("\nDuplex set to Full\n")
else:
print("It's already " + full)

Text file show look something like this:

Use your Router Interface or loopback Ip

Note: Keep the interface in Half-duplex so that when you run the script it changes the duplex setting or if the duplex is already in the full-duplex state then it will show “ It’s already in Full-duplex”

This is Just a Basic one you can add N number of devices and make configurations and extract logs within a very short time. It saves time and avoids mistakes if the script is on point.

--

--

Sadananda.S

Network Engineer | AWS | Python Automation | Cybersecurity