1

I have this 30 virtual machines and I am doing everything manually right now which is a kind of trouble for me. I am trying now to write a script so I can connect to them all automatically and do my desired task. I made a flow diagram of what I need to do. Can anybody just give me some hints on how will i achieve this task programatically. I am attaching the flow diagram.
Thanks in Advance.

enter image description here

Please Right Click On Image and Click View Image to See the Flow Diagram.

4
  • What's your question? You can use paramiko to make ssh connection from python, and everything else is pretty straightforward. Commented Dec 26, 2012 at 11:50
  • 2
    Have a look at Fabric Commented Dec 26, 2012 at 11:52
  • Paramiko to copy files over SSH - code.activestate.com/recipes/… Commented Dec 26, 2012 at 11:53
  • thanks Joddy basically uploading was my problem. Commented Dec 26, 2012 at 11:56

3 Answers 3

4

Reading a text file and getting the data is trivial:

with open('host.txt', 'r') as inf:
    lines = inf.readlines()
    hostlist = [ln.split() for ln in lines]

Now hostlist should be a list of lists;

[['192.168.0.23', 'root', 'secret'], ['192.168.0.24', 'root', 'secret2'] ...

But your list shouldn't have to contain more than the hostnames. IP adresses can be gotten from DNS that you have to configure anyway, and ssh can login without passwords if configured correctly.

Putting the passwords for all your virtual hosts in a plain text file has security concerns. If you want to go that route, make sure to restrict access to that file!

You can use subprocess to execute commands. I would suggest using rsync to push the desired files to the virtual machines. That minimizes network traffic. And you can deploy directly from filesystem to filesystem without having to roll a tarball. It can be as simple as

status = subprocess.check_output(['rsync', '-av', localdir, remotedir])

Where localdir is the directory where the files for the virtual machine in question are stored (it should end with a '/'), and 'remotedir' is the hostname::directory on the virtual machine where the data should land (this should not end with a '/').

For executing commands remotely, ssh is the way to go. Configure that for passwordless login, using ssh's authorized_keys file on each remote host. Then you don't need to put passwords in your list of hosts.

Sign up to request clarification or add additional context in comments.

Comments

1

Fabric is best solution for you. Fabric based on paramiko ( which based on libssh2), it makes very easy to work with commands on remote host and provides function to upload and download fiels from remote host. Here it is http://docs.fabfile.org/en/1.5/

Docs about put function here

Comments

1

I don't get your question in the flow diagram, however you can use paramiko as suggested and I have a large number of background utilities written on top of paramiko which enables support people to monitor remote web servers on the browser. Snippet below,

client = paramiko.SSHClient()
client.set_missing_host_key_policy( paramiko.AutoAddPolicy() )
client.load_system_host_keys()
client.connect( '192.168.100.1', port=4001, username='monitor', password='XXXX' )

cmds = [ "sed -i 's/\/bin\/date -u/\/bin\/date/g' /etc/cron.hourly/reboot" ]

for cmd in cmds:

    if __DEBUG_MODE__:
        print 'Executing..... ' + cmd

    stdin, stdout, stderr = client.exec_command( cmd )

Also if you want to push files below is the snippet,

def setupSFTPClient(self, ip_add):
    print 'Setting SFTP client: ' + ip_add
    tb = 'Finished SFTP-ing. '
    try:
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.load_system_host_keys()
        client.connect(ip_add, port=4001, username='monitor', password='XXXX')
        sftp_client = client.open_sftp()
        # NB you need the filename for remote path otherwise 
        #    paramiko barfs with IOError: Failure
        sftp_client.put( '/home/projects/portal_release.tgz', '/var/ND/portal_release.tgz' )
        sftp_client.put( '/home/projects/portal_installer.sh', '/var/ND/portal_installer.sh' )
        sftp_client.close()

    except Exception, e:
        print e
        tb = traceback.format_exc()

    finally:
        print tb
        client.close()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.