Last active
August 22, 2021 12:47
-
-
Save twr14152/fd9446191b6463857852 to your computer and use it in GitHub Desktop.
Using Python to script the configuration of Cisco devices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# | |
# add your configurations in a list format | |
# | |
host_conf = ['config t', 'interface lo10', 'description TEST-CONFIG-SCRIPT-3', 'end', 'show run int loop 10'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# | |
# 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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
SSH script with seed file and configuration file.