Python: Run Multiple commands on Multiple Remote Servers Simultaneuosly

Yes, sometimes you are in a hurry and need to run multiple commands on multiple servers. Or you just need to make a health checkup script, which will save your time everyday which can be used on other productive tasks, like doing facebook. 

We will be using Fabric module for this. Download it using the official source:

https://pypi.python.org/pypi/Fabric

This works on Python 2.x

-----------------------------------------------------------------------------------

import os, sys
from fabric.api import *

#Then specify host(s) and run the same commands across those systems
 

env.user = 'root'
env.password = 'appletree'

#Define function for first server
def reporters():
    with settings(host_string='192.168.1.12'):
        run("date")
        run("cd /home ; ls ")
    

#Run the function for first server
reporters()





env.user = 'root'     #If username is same, you can omit this line
env.password = 'mangotree'
#If password is same, you can omit this line

#Define function for second server
def reporters():
   
with settings(host_string='192.168.1.13'):
        run("date")
        run("uptime")

        run("df -h")

    

#Run the function for second server
reporters() 

#You can add as many servers as you want.


Comments