blob: 2873cb52e42c249925d2b4c27eea90488ecfc79a [file] [log] [blame]
gstepanove3481cd2015-04-03 21:45:52 +03001import argparse
2import subprocess
3import sys
4
5
6def make_tunnels(ips, base_port=12345, delete=False):
7 node_port = {}
8
9 if delete is True:
10 mode = "-D"
11 else:
12 mode = "-A"
13
14 for ip in ips:
15 p = subprocess.Popen(["iptables -t nat " + mode + " PREROUTING " +
16 "-p tcp -i eth1 --dport " + str(base_port) +
17 " -j DNAT --to " + str(ip) + ":22"],
18 stdin=subprocess.PIPE,
19 stdout=subprocess.PIPE,
20 stderr=subprocess.STDOUT,
21 shell=True)
22
23 out, err = p.communicate()
24
koder aka kdanilovda45e882015-04-06 02:24:42 +030025 if out is not None:
gstepanove3481cd2015-04-03 21:45:52 +030026 print out
27
koder aka kdanilovda45e882015-04-06 02:24:42 +030028 if err is not None:
gstepanove3481cd2015-04-03 21:45:52 +030029 print err
30
31 node_port[ip] = base_port
32 base_port += 1
33
34 return node_port
35
36
37def parse_command_line(argv):
koder aka kdanilovda45e882015-04-06 02:24:42 +030038 parser = argparse.ArgumentParser(description="Connect to fuel master " +
gstepanove3481cd2015-04-03 21:45:52 +030039 "and setup ssh agent")
koder aka kdanilovda45e882015-04-06 02:24:42 +030040 parser.add_argument("--base_port", type=int, required=True)
gstepanove3481cd2015-04-03 21:45:52 +030041 # To do: fix clean to be False when string is False
koder aka kdanilovda45e882015-04-06 02:24:42 +030042 parser.add_argument("--clean", type=bool, default=False)
43 parser.add_argument("--ports", type=str, nargs='+')
gstepanove3481cd2015-04-03 21:45:52 +030044
45 return parser.parse_args(argv)
46
47
48def main(argv):
49 arg_object = parse_command_line(argv)
50 mapping = make_tunnels(arg_object.ports,
51 base_port=arg_object.base_port,
52 delete=arg_object.clean)
53
54 if arg_object.clean is False:
55 for k in mapping:
56 print k + " " + str(mapping[k])
57
58
59if __name__ == "__main__":
60 main(sys.argv[1:])