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() | |
Author
twr14152
commented
Jul 29, 2020
via email
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.
…On Wed, Jul 29, 2020 at 2:12 PM getkmahesh ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
I am looking script which will run below command and provide output
Conf t
ip http client connection forceclose
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
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/fc9639870cc7348f834d#gistcomment-3396953>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABHPENYL4X46465U4TYVTNDR6BRBHANCNFSM4I7WS6ZQ>
.
--
Todd
614/570-2618
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
Typically if it's a new switch meaning not a replacement you would build the switch config manually and then manage from that point forward via automation. However if you have the config ready to go and the device is online and remotely reachable via ssh you could very easily push a config to it using netmiko. It's what I would recommend starting with.
Check out this example on the netmiko GitHub page.
https://github.com/ktbyers/netmiko/blob/develop/examples/config_change_file.py <https://github.com/ktbyers/netmiko/blob/develop/examples/config_change_file.py>
Recommend poking around on this site. The guys great. He has many examples. The one I listed above is simply one of many options.
https://pynet.twb-tech.com/blog/automation/netmiko.html
Good luck.
… On Aug 2, 2020, at 8:38 PM, hassanthiam ***@***.***> wrote:
@hassanthiam commented on this gist.
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
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub <https://gist.github.com/fc9639870cc7348f834d#gistcomment-3401467>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/ABHPENZLZRNEF6QMXUJO2R3R6YBJXANCNFSM4I7WS6ZQ>.
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