Using Python, SSH and Threads to quickly configure Servers
Tuesday, November 10, 2009 at 5:08PM Configuring a bunch of Servers can be a pain, recently I had to setup tc priority queues for limiting the bandwidth and response time on 10 Servers (going in an out), that would mean 100 command line calls, and since they are all different of course doing it via parallel-ssh would not help. Being a Python guy it immediately sprang to mind that there is a good CSV lib, and since 10x10 Servers is a Matrix it can easily be represented in a CSV. I wrote about handling CSV files earlier, so check out Handling CSV Files. The tricky part is the handling of SSH commands in the script, but lucky me, theres a lib for that, it's called Paramiko. With a little abstraction done by Commandline.org it is easily useable to setup connections pass commands and get results. The Tutorial on Commandline.org is actually quite nice to get started on SSH in Python.
The setup so far:
- CSV with 10x10 Rows/Columns including the hostnames and ports to connect to
- A script parsing the CSV and passing the needed commands via ssh.py to the host
The problem:
- It takes forever!
class Controller(Thread):
def __init__(self, var1, var2):
Thread.__init__(self)
self.var1 = var1
self.var2 = var2All there is to do now, is implementing the run method and doing whatever is needed there
def run(self):
do something cool
Now to integrate it in the Main Script
controllers = []
for r in rows:
for c in columns:
current = Controller(r, c)
controllers.append(current)
for i in controllers:
i.join()
All that does is create a separate thread per item, and append to a array of threads. By running join it makes sure that every thread is done before the scripts exists. And thats it, you now got a threaded script running ssh commands on a bunch of hosts.


Reader Comments