-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws_ssh.py
38 lines (32 loc) · 1.26 KB
/
aws_ssh.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
import argparse
import boto3
import os
import sys
from ruamel import yaml
import aws_common
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--profile', default=os.environ.get('AWS_PROFILE', 'default'))
parser.add_argument('--region', default=os.environ.get('AWS_REGION', 'virginia'))
parser.add_argument('--tunnel-ports', type=int, nargs='+')
parser.add_argument('--name', type=str, required=True)
args = parser.parse_args()
region_code_by_name = {
'tokyo': 'ap-northeast-1',
'virginia': 'us-east-1'
}
region_code = region_code_by_name[args.region]
with open('config.yaml') as f:
config = yaml.safe_load(f)
session = boto3.Session(profile_name=args.profile, region_name=region_code)
ec2 = session.client('ec2')
instance = aws_common.get_instance(ec2=ec2, name=args.name)
instance_id = instance['InstanceId']
instance_ip = instance['PublicIpAddress']
key_path = config['key_path']
tunnel_l = []
if args.tunnel_ports is not None:
for port in args.tunnel_ports:
tunnel_l.append(f'-L {port}:localhost:{port}')
tunnel_str = ' '.join(tunnel_l)
print(f'ssh -o StrictHostKeyChecking=no {tunnel_str} -i {key_path} ubuntu@{instance_ip}')