Created
December 14, 2018 17:41
-
-
Save julbrs/a961529651260be1613a725a38109499 to your computer and use it in GitHub Desktop.
Simple script to list/connect via *RDP/SSH* a AWS EC2 instance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
from collections import defaultdict | |
import boto3 | |
import sys | |
import os | |
import time | |
""" | |
A tool for retrieving basic information from EC2 instances. Then when you | |
run it with 'awsco instance-name' it will try to connect it via RDP or SSH | |
The tool is also able to start the EC2 if not started yet. | |
Run with 'AWS_PROFILE=gbs AWS_DEFAULT_REGION=eu-central-1 awsco' to set | |
specific profile/region | |
""" | |
# Connect to EC2 | |
ec2 = boto3.resource('ec2') | |
client = boto3.client('ec2') | |
# Get information for all running instances | |
running_instances = ec2.instances.filter(Filters=[]) | |
ec2info = defaultdict() | |
for instance in running_instances: | |
for tag in instance.tags: | |
if 'Name'in tag['Key']: | |
name = tag['Value'] | |
if 'description'in tag['Key']: | |
description = tag['Value'] | |
# Add instance info to a dictionary | |
ec2info[instance.id] = { | |
'Name': name, | |
'Description': description, | |
'State': instance.state['Name'], | |
'Public IP': instance.public_ip_address, | |
'Launch Time': instance.launch_time | |
} | |
attributes = ['Name', 'Description', 'State', 'Public IP'] | |
if len(sys.argv)==1: | |
print("Instances") | |
for instance_id, instance in ec2info.items(): | |
for key in attributes: | |
print("{0}: {1}".format(key, instance[key])) | |
print("------") | |
else: | |
id = sys.argv[1] | |
instances = ec2.instances.filter(Filters=[{'Name': 'tag:Name', 'Values': [id]}]) | |
if sum(1 for _ in instances.all()) == 1: | |
print("connecting to %s..." % id) | |
for instance in instances: | |
if(instance.state['Code']==80): | |
print("Start the EC2...") | |
response = client.start_instances( | |
InstanceIds=[ | |
instance.id, | |
] | |
) | |
print("Instance started. Waiting 5s before connect...") | |
time.sleep(5) | |
if instance.platform=='windows': | |
password = None | |
name = '' | |
for tag in instance.tags: | |
if 'password'in tag['Key']: | |
password = tag['Value'] | |
if 'Name'in tag['Key']: | |
name = tag['Value'] | |
if password: | |
print("ip: %s" % instance.public_ip_address) | |
os.system("xfreerdp /t:aws:%s /compression /size:1800x1000 /drive:mydisk,/home /clipboard /u:Administrator /p:%s /v:%s /kdb:EN /cert-ignore" % (name, password, instance.public_ip_address)) | |
else: | |
print("no password set please set it !") | |
else: | |
os.system("ssh -i ~/.ssh/gbandsmith.pem -X centos@%s" % instance.public_ip_address) | |
else: | |
print("can't find %s" % id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment