SSH into an ec2 instance using boto

0 votes
How do I ssh into an ec2 instance using boto? What script am I suppossed to use?
Jun 24, 2019 in Python by Neha
3,453 views

1 answer to this question.

0 votes

Hi @Neha, try something like thus:

import boto3
import botocore
import paramiko

key = paramiko.RSAKey.from_private_key_file(path/to/mykey.pem)
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# Connect/ssh to an instance
try:
    # Here 'ubuntu' is user name and 'instance_ip' is public IP of EC2
    client.connect(hostname=instance_ip, username="ubuntu", pkey=key)

    # Execute a command(cmd) after connecting/ssh to an instance
    stdin, stdout, stderr = client.exec_command(cmd)
    print stdout.read()

    # close the client connection once the job is done
    client.close()
    break

except Exception, e:
    print e
answered Jun 24, 2019 by Varsha

Related Questions In Python

0 votes
1 answer

Attach volume to existing ec2 instance using python

Yes of course that's possible with just ...READ MORE

answered Jun 24, 2019 in Python by Krina
1,351 views
0 votes
1 answer

How to import an image into a csv file using python?

I am sure you are aware CSV ...READ MORE

answered Jun 25, 2020 in Python by Bhanu Kumar
7,304 views
0 votes
1 answer

Unable to create EC2 instance using boto3

Hi@Pradeep, I have tried the below code and ...READ MORE

answered Jul 13, 2020 in Python by MD
• 95,440 points
660 views
+2 votes
3 answers

How can I play an audio file in the background using Python?

down voteacceptedFor windows: you could use  winsound.SND_ASYNC to play them ...READ MORE

answered Apr 4, 2018 in Python by charlie_brown
• 7,720 points
12,917 views
0 votes
1 answer

Using Shapely on AWS Lambda with Python 3

For some reason, the pip install of ...READ MORE

answered Oct 8, 2018 in AWS by Priyaj
• 58,090 points
2,590 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Create an ec2 instance using boto

Hi @Neel, try this script: reservations = conn.get_all_instances(instance_ids=[sys.argv[1]]) instances ...READ MORE

answered Jun 24, 2019 in Python by Varsha
627 views
0 votes
1 answer

I'm using Python 2.7 to convert an XML response (from a REST call to Atlassian Fisheye) into an HTML table.

You don't have a template matching tabularQueryResult in your ...READ MORE

answered Oct 4, 2018 in Python by Priyaj
• 58,090 points
1,116 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP