Last active
December 31, 2022 19:08
-
-
Save twr14152/fc9639870cc7348f834d to your computer and use it in GitHub Desktop.
python ssh script to automate gathering info and pushing configs to Cisco routers and switches
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
host_conf = ['show ip arp vlan 11', 'show ip arp vlan 12', 'show ip arp vlan 13', 'show ip arp vlan 32', 'show ip arp vlan 50', 'show ip arp vlan 102', 'show ip arp vlan 112', 'show ip arp vlan 145', 'show ip arp vlan 150', 'show ip arp vlan 155', 'show ip arp vlan 160', 'show ip arp vlan 161', 'show ip arp vlan 171', 'show ip arp vlan 172', 'show ip arp vlan 327', 'show ip arp vlan 328', 'show ip arp vlan 331', 'show ip arp vlan 332', 'show ip arp vlan 374', 'show ip arp vlan 380', 'show ip arp vlan 401', 'show ip arp vlan 409', 'show ip arp vlan 433', 'show ip arp vlan 434', 'show ip arp vlan 435', 'show ip arp vlan 436', 'show ip arp vlan 334', 'show ip arp vlan 343', 'show ip arp vlan 403', 'show ip arp vlan 404', 'show ip arp vlan 406', 'show ip arp vlan 447','exit'] | |
# To implement config changes your config would be implemented in 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
network_devices = ['x.x.x.4', 'x.x.x.5'] |
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
__author__ = "ToddR" | |
# | |
# | |
import paramiko | |
import time | |
import getpass | |
import os | |
from host_file import network_devices | |
from config_file import host_conf | |
UN = raw_input("Username : ") | |
PW = getpass.getpass("Password : ") | |
# For loop allows you to specify number of hosts | |
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 allows you to specify number of commands you want to enter | |
#Dependent on the output of the commands you may want to tweak sleep time. | |
for command in host_conf: | |
remote.send(' %s \n' % command) | |
time.sleep(2) | |
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
Thank You !!! I ll let you know how it goes .