Skip to content

Instantly share code, notes, and snippets.

@twr14152
Last active December 31, 2022 19:08
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • 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()
@djk80172
Copy link

Thanks for this! Exactly what I was looking for! A couple of things for those who are trying to use this in Python 3 that I found:

In line 12, raw_input was renamed in Python 3 as just "input".

In lines 18 and 31 the print command syntax changed so they should be print(ip) and print(buf), respectively.

In line 32, the file has to be opened as binary to make this script work. So the 'a' parameter should be 'ab'.

@sushantaBehera
Copy link

Python can not support to push the switch/router configuration script in a normal text format , not in a list format ? like how we copy and paste the configuration script from the normal text file to our switch/router console , the same file wants to push through Python . Please share your inputs here

@twr14152
Copy link
Author

twr14152 commented Oct 11, 2019

Not sure I'm following your question. This is a really old script (close to 5 years old) I wrote in python2 and I would not do it like this again. I was learning how things worked when I did it. However the list format will get looped through line by line in the for loop "for command in host_conf:". each iteration through the list will look like normal text as you stated by the router.

If your going to look at some examples I would check out the stuff on my GitHub. I think there more up to date and probably a little clearer, and they are written in python3.
See below.
https://github.com/twr14152/Network-Automation-Scripts_Python3

@sushantaBehera
Copy link

sushantaBehera commented Oct 11, 2019 via email

@twr14152
Copy link
Author

twr14152 commented Oct 11, 2019

Absolutely. That's how I would do it. You could also look Into jinja templates to take it a step further. That way your writing the script once updating config files / templates as necessary. The first link below shows how to connect to multiple device each with different config files. Did it for my lab at the time but you should be able to glean what you want from it.

https://github.com/twr14152/Network-Automation-Scripts_Python3/blob/master/netmiko/config_scripts/multiple_device_config_script.py

Another example using juniper devices.
https://github.com/twr14152/Network-Automation-Scripts_Python3/blob/master/netmiko/Juniper/juniper_conf_from_file.py

Hope that helps. Sorry for the confusion. The interactive scripts your referencing are good for troubleshooting and testing in lab. That's about it. I would use static files for config in production. That way they can go through a change management process.

@ste-giraldo
Copy link

ste-giraldo commented Nov 24, 2019

I added:
os.remove("config_file.pyc")
os.remove("host_file.pyc")
print("Temp files removed")

at the end, in order to remove temp files created during execution.

Changed also the sleep time to "0.5".

@twr14152: host_file and config_file can work also as a list, which is more common that in line, so my files are:
$ cat config_file.py
host_conf = [
'conf t',
'line vty 0 15',
'transport input all',
'end',
'wr',
]

$ cat host_file.py
network_devices = [
'10.25.64.40',
'10.25.64.36',
]

@getkmahesh
Copy link

getkmahesh commented Jul 29, 2020

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

@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