-
Notifications
You must be signed in to change notification settings - Fork 16
/
run_create_ec2_keypair.py
executable file
·112 lines (86 loc) · 2.88 KB
/
run_create_ec2_keypair.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
#!/usr/bin/env python3
import os
import subprocess
from multiprocessing import Process
env = dict(os.environ)
def read_file(file_path):
f = open(file_path)
lines = list()
for ll in f.readlines():
lines.append(ll)
f.close()
return lines
# noinspection PyShadowingNames
def run(cmd, file_path_name=None, cwd=None):
def _f():
if not file_path_name:
_p = subprocess.Popen(cmd, cwd=cwd, env=env)
_p.communicate()
if _p.returncode != 0:
raise Exception()
else:
with open(file_path_name, 'a') as f:
_p = subprocess.Popen(cmd, stdout=f, cwd=cwd, env=env)
_p.communicate()
if _p.returncode != 0:
raise Exception()
pp = Process(target=_f)
pp.start()
pp.join()
if pp.exitcode != 0:
raise Exception()
def run_create_ec2_keypair(key_name):
cmd = ['rm', '-f']
cmd += [f'{key_name}.pem']
cmd += [f'{key_name}.pub']
run(cmd)
cmd = ['openssl', 'genrsa']
cmd += ['-out', f'{key_name}.pem']
cmd += ['2048']
run(cmd)
print('Private key file:', f'{key_name}.pem', '\n')
run(['chmod', '400', f'{key_name}.pem'])
cmd = ['openssl', 'rsa']
cmd += ['-in', f'{key_name}.pem']
cmd += ['-pubout']
run(cmd, file_path_name=f'{key_name}.pub')
print('Public key file:', f'{key_name}.pub', '\n')
print('AWS_KEY_PAIR_NAME:', key_name, '\n')
pub_key = read_file(f'{key_name}.pub')
pub_key = pub_key[1:-1]
pp_list = list()
for pk in pub_key:
pk = pk.strip()
pp_list.append(pk)
pub_key = ''.join(pp_list)
print('AWS_KEY_PAIR_MATERIAL:', pub_key, '\n')
run(['chmod', '400', f'{key_name}.pub'])
cmd = ['ssh-keygen', '-y']
cmd += ['-f', f'{key_name}.pem']
result, error = subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE).communicate()
# noinspection PyUnresolvedReferences
result = result.decode('utf-8')
print('OpenSSH public key:', result.strip(), '\n')
cmd = ['openssl', 'pkey']
cmd += ['-in', f'{key_name}.pem']
cmd += ['-pubout']
cmd += ['-outform', 'DER']
proc = subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE)
cmd = ['openssl', 'md5', '-c']
result, _ = subprocess.Popen(cmd, env=env, stdin=proc.stdout, stdout=subprocess.PIPE).communicate()
# noinspection PyUnresolvedReferences
result = result.decode('utf-8')
print('OpenSSH finger print:', result)
return pub_key
################################################################################
#
# start
#
################################################################################
if __name__ == "__main__":
from run_common import parse_args
_, args = parse_args()
if len(args) != 2:
print('usage:', args[0], '<key-name>')
raise Exception()
run_create_ec2_keypair(args[1])