python + ssh (paramiko) + N servers + M commands

Recently I was asked by a client to basically run a block of commands on 100+ servers. All the servers had the same commands to be executed as well as my user account was the same for all the boxes, which is convenient.

Anyways, when they asked me to do this they had no automated scripts to do this ( we’re modernizing their environment 🙂 ) and I’m thinking to myself, ‘there is no way I’m going to manually log into these boxes to run these commands’. Then I briefly contemplated whether or not I wanted to write a bash script to do the job… And I was like no thanks. Basically it was for the same reasons why I avoid bash at all costs (clunky, cryptic and touchy), no offense my bash brethren.

Python, from my perspective is amazing. It is easy to use, easy to understand, Python has a huge library (modules), user community is vast, and to do complicated sys admin or development you don’t need be a wizard. I have a slew of colleagues who swear by ruby and I have used it briefly, but it just doesn’t have the same feel and intuitiveness that Python does. WTS, I bet once I learned the syntax better I’d probably like it equally was well, but eh, for now Python and it does everything I want/need, quickly!

Backkkk to my post about python + ssh (paramiko), okay whilst reading Python for Unix and Linux System Administration by Noah Gift and Jeremy Jones (great book, highly recommend) on SSH tools I came across Paramiko. And wow, what a great simple way to extend ssh tooling to Python.

Using Paramiko I was able to set up a script that churned through all 100+ servers with a list of commands and log the results in a matter of minutes. Of course this doesn’t include testing, because I didn’t want to test on live systems at least not if I wanted to keep my contract 🙂 .

Check out my code on github, it is extremly straightforward (assuming you are familiar with the basic tenants of SSH and remote administration).

https://github.com/jmarley/python-ssh-remote-cmd-exec

Happy coding!

Advertisements

Author: jasonmarley

I have been with Red Hat since 2010 and love it! My day to day is consulting on RHEL/JBoss/OpenShift, but I work on open source projects in my free time. The best part about my job are my awesome colleagues and our community.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s