-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec2ssm.py
executable file
·128 lines (105 loc) · 3.92 KB
/
ec2ssm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python3
# -*- coding: utf-8 -*
import boto3
import json
import sys
import pickle
import os
from collections import defaultdict
import pexpect
import subprocess
import argparse
# get Name tag
def parse_sets(tags):
result = {}
for tag in tags:
key = tag['Key']
val = tag['Value']
result[key] = val
return result
# get instance with instance name for update
def get_ec2_instance_list(profile="default"):
instance_list = []
instances = get_instances(profile)
for reservations in instances['Reservations']:
for instance in reservations['Instances']:
tags = parse_sets(instance['Tags'])
instance_list.append(tags['Name'])
return instance_list
# get instance with instance name for update
def update_ec2_instance_list(profile="default"):
instance_list = []
instances = update_instances(profile)
for reservations in instances['Reservations']:
for instance in reservations['Instances']:
tags = parse_sets(instance['Tags'])
instance_list.append(tags['Name'])
return instance_list
# update cache
def update_instances(profile="default"):
session = boto3.session.Session(profile_name=profile)
ec2 = session.client('ec2')
instances = ec2.describe_instances()
pickle.dump(instances, open(os.path.dirname(os.path.abspath(__file__)) + "/" + profile + "_instances.cache", "wb"))
return instances
# use cache
def get_instances(profile="default"):
try: instances = pickle.load(open(os.path.dirname(os.path.abspath(__file__)) + "/" + profile + "_instances.cache", "rb"))
except (OSError, IOError) as e:
session = boto3.session.Session(profile_name=profile)
ec2 = session.client('ec2')
instances = ec2.describe_instances()
pickle.dump(instances, open(os.path.dirname(os.path.abspath(__file__)) + "/" + profile + "_instances.cache", "wb"))
return instances
## find instance with instance name
def find_ec2_instanceid(instance_name, profile_name):
instances = get_instances(profile_name)
for reservations in instances['Reservations']:
for instance in reservations['Instances']:
tags = parse_sets(instance['Tags'])
if tags['Name'] == instance_name:
return instance['InstanceId']
def parse_sets(tags):
result = {}
for tag in tags:
key = tag['Key']
val = tag['Value']
result[key] = val
return result
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("arg1")
parser.add_argument("--profile", help="set profile")
args = parser.parse_args()
if not args.profile :
profile = "default"
else:
profile = args.profile
if args.arg1 == 'update':
instance_list = update_ec2_instance_list(profile)
instances_txt = ' '.join(map(str, instance_list))
path = os.environ['HOME'] + '/.aws_instances_' + profile
with open(path, mode='w') as f:
f.write(instances_txt + ' ')
exit()
instance_name = args.arg1
instance_id = find_ec2_instanceid(instance_name, profile)
if instance_id == None:
print("instance not found, please check Name tag and environment")
exit()
stty_size = subprocess.check_output(['stty','size']).decode('utf-8').strip().split()
rows = stty_size[0]
cols = stty_size[1]
prc = pexpect.spawn("aws ssm start-session --region ap-northeast-1 --target " + instance_id + " --profile " + profile)
if 'TMUX' in os.environ:
os.system("tmux rename-window " + instance_name)
if 'EC2USER' and 'EC2PASSWORD' in os.environ:
ec2user= os.environ['EC2USER']
ec2password = os.environ['EC2PASSWORD']
prc.expect('$')
prc.sendline('su - ' + ec2user)
prc.expect("Password:")
prc.sendline(ec2password )
prc.expect('$')
prc.sendline("stty rows " + rows + " cols " + cols)
prc.interact()