Question

I am able to fire up AWS Ubuntu EC2 instance with boto. Have anyone tried to upload the script to the remote Ubuntu EC2 (More than 1) and execute the script via SSH locally?

The main objective is to automate the whole process using a Python script written on localhost. Is there an alternative way or Amazon api tools to made this possible?

Was it helpful?

Solution 2

Use paramiko API

OTHER TIPS

I'd recommend Fabric, it's made for this kind of thing.

Here, Paramiko code to execute in remote AWS EC2 Python :

import paramiko
sftp, transport= None, None,  None
try:
    if keyfilepath=='': keyfilepath= AWS_KEY_PEM
    if keyfiletype == 'DSA':  key = paramiko.DSSKey.from_private_key_file(keyfilepath)
    else:                     key = paramiko.RSAKey.from_private_key_file(keyfilepath)

    if contype== 'sftp' :
      transport = paramiko.Transport((host, port))
      transport.add_server_key(key)
      transport.connect(None, username,  pkey=key)
      sftp = paramiko.SFTPClient.from_transport(transport)
      if isprint : print('Root Directory :\n ', sftp.listdir())
      return sftp

except Exception as e:
    print('An error occurred creating client: %s: %s' % (e.__class__, e))
    if sftp is not None:      sftp.close()
    if transport is not None: transport.close()
    if ssh is not None: ssh.close()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top