Need a Python script that will copy switch configs from and to 8 network switches through TFTP

Hi guys,

I'm looking for a Pythonscript that will copy Network switch config scripts to and from a TFTP server using HP switches. What I want to do is basically run the script from a TFTP server that will automatically ssh to an HP procurve switch and copy its startup-config back to my tftp server and load a new one from files I have specified. When it does this, it usually does it from one computer which is connected to a switch and when a certain switch goes down it reboots the network and a network respan occurs. When a network respan occurs I can not goto the next switch to copy switch configs until that switch comes back up. Do i need a delay command to wait before it goes to the next switch config or can i use a while loop that will basically copy switch configs from each switch let them reboot and take it out of a group of "to be configured switches" until all of them are done. What I have is a environment that has configs that constantly change and I need a way to basically run one script that will load a set of 8 switch configs fast instead of me taking cable to each serial port on the individual switches, which is really slow. Ill show you what I currently have but it is not as consistent and usually hangs due to a network respan. I'm a little new to Python so excuse my basic code. Is there anything I can do to make my code more reliable and my program closer to the one button configure and copy/load configs in a few seconds scirpt I want?
#!/usr/bin/env python
import subprocess



#Bindividual expect scripts that communicate with each switch 


SW1 = "/tftpboot/ssh_toSW1.exp testing 192.168.1.101"
SW2 = "/tftpboot/ssh_toSW2.exp testing 192.168.1.1"
SW3 = "/tftpboot/ssh_toSW3.exp testing 192.168.1.212"
SW4 = "/tftpboot/ssh_toSW4.exp testing 192.168.1.101"
SW5 = "/tftpboot/ssh_toSW5.exp testing 192.168.1.2"
SW6 = "/tftpboot/ssh_toSW6.exp testing 192.168.1.104"
SW7 = "/tftpboot/ssh_toSW7.exp testing 192.168.1.110"
SW8 = "/tftpboot/ssh_toSW8.exp testing 192.168.1.103"

#Places variables into a list/array and executes them in order from left to right
cmds = [SW1, SW2, SW3, SW4, SW5, SW6, SW7, SW8,]

#Iterates over list, running statements for each item in the list
#Note, that whitespace is absolutely critical and that a consistent indent must be maintained for the code to work properly
count=0
for cmd in cmds:
    count+=1
    print "Running Command Number %s" % count
    subprocess.call(cmd, shell=True)

Open in new window

tbalzAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

shukalo83Commented:
I can not help you beacuse I'm not so sure about topology. Can you explain it a bit or post a scheme? Why is network respanning so problematic if you have for instance one switch (that you do not touch at all) and, as a star topology, all aother switches connected to it. I presume that this is some kind of test environment.
0
tbalzAuthor Commented:
Hi,

Sorry let me clear things up a bit. My script seeks to touch all switches at once and runs as a automated way to load switch configurations by running one script. However my script runs each switch one at a time and I have to make sure whatever host is connected to the switch that is communicating through ssh and tftp needs to be the last one that gets the config before rebooting. The problem is the switches do down one after another and due to network respanning the script just hangs there because it can't communicate with the next switch after one switch goes down. I hope this makes sense. Also I believe my topology is a mesh topology but i don't think it matters because if one switch goes down, the entire network goes down for a little bit due to respans.
0
shukalo83Commented:
Thank You on clarifying this. Maybe the way to go would be to make script a bit smarter by checking connectivity first for, lets say 2 times with 2 minutes brake between, and then apply config on switch. Thing that worries me is that whole concept is not so great and I think that you really have network topology issue with all this respanning. I can always help you with script if you like, altough I'm not a python expert I did make scripts of my own for changing config on cisco switches in bulk.  Gettign back on subject here, I think you need importing ping module (you could do it with os ping command but I prefer this cleaner stuff because you can use exceptions). However, you will learn that python ping modules are far from simple, that there are a few forks on github and that some of them do not even work in linux.

Try with this:

https://github.com/samuel/python-ping/blob/master/ping.py

And try like this:
import ping
import time
for switchip in switchiplist:

try:
    pingtry = ping.do_one('www.google.com', timeout=2)
    upload config
except socket.error, e:
    print "Ping Error:", e
    time.sleep(30) wait for 30s and then try again

I hope it helps
0
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

tbalzAuthor Commented:
Wow thanks! This is the type of stuff I was looking for. I could use a little help with the script if possible. So this script will run through until all the switches in my list have received their updated configs? Also of note: The respan problem is not as bad now when I think about it. It does effect me when im doing 8 switches because 4 would reboot back to back and then 1 switch would hang up due to the respan. I basically need to get smart delays in there that would prevent that from happening.
0
tbalzAuthor Commented:
Can i use the example code from above? How would i define switchip exactly?
0
shukalo83Commented:
Put it like this:

switchiplist = [192.168.1.1,192.168.1.2, 192.168.1.3]

switchip will take values of every ip address. When you say:

for switchip in switchiplist:
       print switchip

just try it, you'll see.

If you need some threading let me know, I have some examples. With threading you'll make it super fast.

Leave the switch that causes problems for last. Make groups of switches that, together do not make problems and do it in one pass. Wait for finish, test it with ping and do another group. In the end take the last one and do it.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shukalo83Commented:
Sorry.

It must be like this switchiplist = ["192.168.1.1", "192.168.1.2", "192.168.1.3"]
0
tbalzAuthor Commented:
Thanks again! I would really like to use threading, can you share some examples?
0
tbalzAuthor Commented:
I wonder if its possible to do all switches at one time with the help of threading? What do you think?
0
shukalo83Commented:
To be honest, you can use threading as an exercise but in small environment you don't really need it.
Besides, threading would be good to change some of the config, not all of it and  then to reboot because you loose a great deal on rebooting. So threading does not really help here.

I've use it successfully with about 30 - 40 switches in production environment. I'm not really proud on the code because I'm not a developer but it worked. I would kindly suggest to you to try and complete this task and learn in the process. That will give you more experience and confidence. After this you can try to make python manage working config during telnet or ssh session. You can always ask me something if things go wrong.

So,
Make this work, have a look at pexpect module and come again here and we can make something together.

Best regards!

0
tbalzAuthor Commented:
You ideas really put me in the right direction but i'm stuck at the moment because my python script wont finish executing. Do you have any idea what could cause them to be stuck?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.