Skip to content

Instantly share code, notes, and snippets.

@twr14152
Last active December 31, 2022 19:08
Show Gist options
  • Save twr14152/fc9639870cc7348f834d to your computer and use it in GitHub Desktop.
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
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']
network_devices = ['x.x.x.4', 'x.x.x.5']
__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()
@twr14152
Copy link
Author

I'll post link for it when I get to it later today.

-Todd

@twr14152
Copy link
Author

I am looking script which will run below command on cisco switch and provide output
#Conf t
(Confg)#ip http client connection foreclose
(Config)#end
#sh ip http client all

this I want to run on multiple IP and want to create an output file for all file can I use script shown

Try this this.
https://gist.github.com/twr14152/fd5531d28d3d62b934e26996b590029c

It calls a host_file.txt which you will need to create for your hosts. Its also assuming you are connecting to cisco devices, there's also the assumption you are connecting with a common user name and password for all devices. You will probably need to tweak it to meet your needs but it should do the job. If you want to test it out against the cisco devnet devices simply try running the script as is. I used the netmiko and I'm running python3.8. I would imagine anything after 3.6 would work.

@twr14152
Copy link
Author

twr14152 commented Jul 29, 2020 via email

@hassanthiam
Copy link

Hi

I m just starting up in Network Automation. Could you please advise on the best method to push a whole switch config ( from a text file ) into a device ?

Any feedback would be much appreciated

Thanks

@twr14152
Copy link
Author

twr14152 commented Aug 3, 2020 via email

@hassanthiam
Copy link

Thank You !!! I ll let you know how it goes .

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