Skip to content

Instantly share code, notes, and snippets.

@twr14152
Last active August 22, 2021 12:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twr14152/fd9446191b6463857852 to your computer and use it in GitHub Desktop.
Save twr14152/fd9446191b6463857852 to your computer and use it in GitHub Desktop.
Using Python to script the configuration of Cisco devices
#
#
# add your configurations in a list format
#
host_conf = ['config t', 'interface lo10', 'description TEST-CONFIG-SCRIPT-3', 'end', 'show run int loop 10']
#
#
# Add the host addresses you want to log into
#
#network_devices = ['x.x.x.1', 'x.x.x.2', 'x.x.x.3', 'x.x.x.4']
#
network_devices = ['10.13.0.1', '10.13.0.2']
import paramiko
import time
import getpass
import os
from host_seedfile import network_devices
from host_config_file import host_conf
UN = raw_input("Username : ")
PW = getpass.getpass("Password : ")
#This For loop calls host list in the seed file "hostlist_script.py"
for ip in network_devices:
print ip
twrssh = paramiko.SSHClient()
twrssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
twrssh.connect(ip, port=22, username=UN, password=PW)
remote = twrssh.invoke_shell()
remote.send('term len 0\n')
time.sleep(1)
#This for loop uses config file 'host_config1.py' to enter commands one by one
for command in host_conf:
remote.send(' %s \n' % command)
time.sleep(1)
buf = remote.recv(65000)
print buf
f = open('sshlogfile0001.txt', 'a')
f.write(buf)
f.close()
twrssh.close()
@twr14152
Copy link
Author

SSH script with seed file and configuration file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment