Showing posts with label Fabric. Show all posts
Showing posts with label Fabric. Show all posts

Wednesday, April 8, 2015

Running System Commands Against Multiple SSH Servers with Fabric

Fabric is a python library to automate tasks

As the README says:
Fabric is a Python (2.5-2.7) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.
 
More specifically, Fabric is:
A tool that lets you execute arbitrary Python functions via the command line;
A library of subroutines (built on top of a lower-level library) to make executing shell commands over SSH easy and Pythonic.

http://docs.fabfile.org/en/latest/tutorial.html

Quick and dirty script to get the same output as we did with the Metasploit post

$cat fab_ssh.py

from fabric.api import run,env

env.hosts = ['root@192.168.1.50:22', 'root@192.168.1.51:22']
env.passwords = {'root@192.168.1.50:22': 'password1, 'root@192.168.1.51:22': 'password2'}

def host_uptime():
    run('uptime')


And now lets run it

$fab host_uptime -f fab_ssh.py
from fabric.api import run,env
[root@192.168.1.50:22] Executing task 'host_uptime'
[root@192.168.1.50:22] run: uptime
[root@192.168.1.50:22] out:  07:08:26 up 22 days, 11:12,  1 user,  load average: 0.00, 0.03, 0.05
[root@192.168.1.50:22] out:

[root@192.168.1.51:22] Executing task 'host_uptime'
[root@192.168.1.51:22] run: uptime
[root@192.168.1.51:22] out:  07:08:32 up 22 days, 11:12,  1 user,  load average: 0.07, 0.02, 0.00
[root@192.168.1.51:22] out:


Done.
Disconnecting from root@192.168.1.50... done.
Disconnecting from root@192.168.1.51... done.