#!/usr/bin/python

import paramiko
import cmd

class RunCommand(cmd.Cmd):
    """ Simple shell to run a command on the host """

    prompt = 'ssh > '

    def __init__(self):
        cmd.Cmd.__init__(self)
        self.hosts = []
        self.connections = []

    def do_add_host(self, args):
        """add_host
        Add the host to the host list"""
        if args:
            self.hosts.append(args.split(','))
        else:
            print "usage: host "

    def do_connect(self, args):
        """Connect to all hosts in the hosts list"""
        for host in self.hosts:
            client = paramiko.SSHClient()
            client.set_missing_host_key_policy(
                paramiko.AutoAddPolicy())
            client.connect(host[0],
                username=host[1],
                password=host[2])
            self.connections.append(client)

    def do_run(self, command):
        """run
        Execute this command on all hosts in the list"""
        if command:
            for host, conn in zip(self.hosts, self.connections):
                stdin, stdout, stderr = conn.exec_command(command)
                stdin.close()
                for line in stdout.read().splitlines():
                    print 'host: %s: %s' % (host[0], line)
        else:
            print "usage: run "

    def do_close(self, args):
        for conn in self.connections:
            conn.close()

if __name__ == '__main__':
    RunCommand().cmdloop()
@@ -1400,16 +1400,533 @@
 lose()%0A%0A
+    def do_run_file(self, file):%0A        ssh = paramiko.SSHClient()%0A        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())%0A        ssh.connect(self.hosts%5B0%5D%5B0%5D,%0A                    username=self.hosts%5B0%5D%5B1%5D,%0A                    password=self.hosts%5B0%5D%5B2%5D)%0A        ftp = ssh.open_sftp()%0A        ftp.put(file, 'remotefile.sh')%0A%0A        stdin, stdout, stderr = ssh.exec_command(%22sh remotefile.sh%22)%0A        for line in stdout.read().splitlines():%0A            print line%0A        ftp.close()%0A        ssh.close()%0A%0A
 if __nam
@@ -1970,8 +1970,9 @@
 mdloop()
+%0A

connect to host and run a sh script