-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_tunnels.py
executable file
·65 lines (52 loc) · 2.14 KB
/
docker_tunnels.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
#!/usr/bin/env python
import re
import os
import glob
import json
import struct
import socket
from collections import defaultdict
scriptname = os.path.basename(__file__)
ip2int = lambda ipstr: struct.unpack('!I', socket.inet_aton(ipstr))[0]
int2ip = lambda n: socket.inet_ntoa(struct.pack('!I', n))
lo_used = []
for hosts in ['/etc/hosts',] + glob.glob('/etc/hosts.d/*'):
with open(hosts) as f:
lo_used += [ip2int(ip) for ip in re.findall(r'127\.[0-9]+(?:\.[0-9]+){2}', f.read())]
def new_lo(s):
return s + 1 if s + 1 not in lo_used else new_lo(s + 1)
def make_script(json_file, scriptname):
tunnels_json = json.load(open(json_file))
tunnel_sh = '#!/usr/bin/env bash\n# AUTO GENERATED from %(scriptname)s (source: %(json_file)s)\n\n' % vars()
hosts_append = '# GENERATED FROM %(scriptname)s (source: %(json_file)s)\n' % vars()
if "opts" in tunnels_json.keys():
tunnel_sh += 'ssh %(opts)s \\\n' % tunnels_json
else:
tunnel_sh += 'ssh -M -S %s.socket -fnNT \\\n' % ''.join(json_file.split('.')[:-1]).replace('/tunnels/', '/root/')
if "identity_file" in tunnels_json:
tunnel_sh += "\t-i %(identity_file)s \\\n" % tunnels_json
hosts = defaultdict(list)
ordered = []
for tunnel in tunnels_json["tunnels"]:
host, port = tunnel.split(':')
hosts[host].append(port)
if host not in ordered:
ordered.append(host)
seed = ip2int('127.0.0.1')
for tunnel in ordered:
seed = new_lo(seed)
lo_used.append(seed)
lo = int2ip(seed)
for port in hosts[tunnel]:
tunnel_sh += "\t-L %(lo)s:%(port)s:%(tunnel)s:%(port)s \\\n" % vars()
hosts_append += "%(lo)s\t%(tunnel)s\n" % vars()
tunnel_sh += '\t%(user)s@%(jump_gateway)s\n' % tunnels_json
return tunnel_sh, hosts_append
# Do the work
for json_file in glob.glob("tunnels/*.json"):
script, hosts = make_script(json_file, scriptname)
fname = os.path.splitext(os.path.basename(json_file))[0]
with open('tunnels/%s.sh' % fname, 'w') as f:
f.write(script)
with open('etc/hosts.d/%s.hosts' % fname, 'w') as f:
f.write(hosts)