-
Notifications
You must be signed in to change notification settings - Fork 1
/
update_ssh_config.py
executable file
·60 lines (46 loc) · 1.69 KB
/
update_ssh_config.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
#!/usr/bin/env python3
import simplejson as json
import argparse
import os
import requests
import sys
import time
import urllib3
import subprocess
secret_key = "4htwt6MCV9oGTOpaTS0"
url = "https://beta.iwe.cloud/backend/servers/query/by-tags"
headers = {"secret-key": secret_key, "tags": "all"}
ssh_dir = os.path.expanduser('~/.ssh/iwe')
ssh_config_file = os.path.expanduser('~/.ssh/config')
ssh_config_template = "Host {}\n\tuser root\n\tIdentityFile {}\n\tHostName {}\n\tUserKnownHostsFile /dev/null\n\tStrictHostKeyChecking no\n"
hosts_file_path = os.path.expanduser('~/dev/iwesible/hosts')
def get_server_list():
try:
request = requests.get(url, headers=headers)
except (urllib3.exceptions.NewConnectionError, requests.exceptions.ConnectionError):
print ('Connection error!')
return None
if request.status_code == 200:
return request.json()
else:
return None
def main():
servers = get_server_list()
if not servers:
print("Exiting...")
sys.exit(-1)
with open(hosts_file_path, 'w+') as hosts_file, \
open(ssh_config_file, 'w+') as ssh_conf:
hosts_file.write("[all]\n")
for server in servers:
id_rsa_path = ssh_dir + "/id_rsa_{}".format(server)
with open(id_rsa_path, 'w+') as ssh_priv:
ssh_priv.write(servers[server]['sshkey_priv'])
os.chmod(id_rsa_path, 0o600)
ssh_conf.write(ssh_config_template.format(server,
ssh_dir + "/id_rsa_{}".format(server),
servers[server]['vpn_ipv4']))
hosts_file.write("{}\n".format(server))
os.chmod(ssh_config_file, 0o600)
if __name__ == "__main__":
main()