gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 1 | import argparse |
| 2 | import subprocess |
| 3 | import sys |
| 4 | |
| 5 | |
| 6 | def 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 kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 25 | if out is not None: |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 26 | print out |
| 27 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 28 | if err is not None: |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 29 | print err |
| 30 | |
| 31 | node_port[ip] = base_port |
| 32 | base_port += 1 |
| 33 | |
| 34 | return node_port |
| 35 | |
| 36 | |
| 37 | def parse_command_line(argv): |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 38 | parser = argparse.ArgumentParser(description="Connect to fuel master " + |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 39 | "and setup ssh agent") |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 40 | parser.add_argument("--base_port", type=int, required=True) |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 41 | # To do: fix clean to be False when string is False |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 42 | parser.add_argument("--clean", type=bool, default=False) |
| 43 | parser.add_argument("--ports", type=str, nargs='+') |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 44 | |
| 45 | return parser.parse_args(argv) |
| 46 | |
| 47 | |
| 48 | def 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 | |
| 59 | if __name__ == "__main__": |
| 60 | main(sys.argv[1:]) |