Python – Cisco CLI Config Changer

Here’s a script I worked on to help make changes to a Cisco devices via CLI. It’s an easy way to automate simple changes. This script also dumps the updated configuration to a text file.

There are three python files.
1. cli-config-changer.py = This is the main file that you run
2. config_file.py = These are the commands you want to run
3. network_devices.py = These are the devices you want to run the commands on

cli-config-changer.py

# Author: Kerry Cordero
import paramiko
import time
import getpass

from network_devices import ip_addresses
from config_file import commands

ID = input("Username : ")
PW = getpass.getpass("Password : ")

# For loop allows you to specify number of hosts
for ip in ip_addresses:
    print(ip)
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(ip, port=22, username=ID, password=PW)
    remote = ssh.invoke_shell()
    remote.send('term len 0\n')
    time.sleep(1)

    # Allows you to specify number of commands you want to enter.  Might need to change sleep timer.
    for command in commands:
        remote.send(' %s \n' % command)
        time.sleep(2)
        output = remote.recv(65000)
        print(output)
        logfile = open('configdump-' + ip + '.txt', 'wb')
        logfile.write(output)
        logfile.close()
    ssh.close()

config_file.py

# Put the commands you want to run.
#commands = ['config t', 'no snmp-server community TEST RO', 'exit', 'wr mem', 'show run']
commands = ['show run']

network_devices.py

# List IP Address you want to connect to.  Exp ['4.4.4.4', '8.8.8.8']
ip_addresses = ['172.18.207.211', '172.30.207.252']
More Stories
AWS & Azure VPN Tunnel Bandwidth Limitation for a Gateway