-
Notifications
You must be signed in to change notification settings - Fork 0
/
forwarder.py
57 lines (44 loc) · 1.45 KB
/
forwarder.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
'''
The script is used to set iptables rules to forward VM traffic to
remote machine like GCP.
'''
import sys, os
from multiprocessing import Process, Lock
from cmdManager import CmdManager
def print_help():
print('Usage: python3 forwarder.py -r N_MIN:N_MAX -a DES_ADDRESS')
print(' [-d DPORT_BASE_HOST] [-p DES_PORT_BASE_VM] [-clear]')
exit(0)
def main():
# parse the parameters
opts = {'-r':'0:5', '-d':5000, '-p':5000, '-a':'0.0.0.0'}
do_clear = False
cur_opt = None
for opt in sys.argv[1:]:
if opt == '-clear':
do_clear = True
elif opt in opts:
cur_opt = opt
elif cur_opt in opts:
opts[cur_opt] = opt
cur_opt = None
else:
print_help()
n_min, n_max = [int(n) for n in opts['-r'].strip().split(':')]
des_ip = opts['-a']
dport_base = int(opts['-d'])
des_port_base = int(opts['-p'])
cmdor = CmdManager(dry_run=False)
for i in range(n_min, n_max + 1):
dport = dport_base + i
des_port = des_port_base + i
if not do_clear:
cmdor.set_iptable_nat(dport, des_ip, des_port)
print('Forward flow %s to %s:%s...' % (i, des_ip, des_port))
else:
cmdor.del_iptable_nat(dport, des_ip, des_port)
print('Delete NAT fule %s to %s:%s...' % (i, des_ip, des_port))
if __name__ == "__main__":
if len(sys.argv) < 2:
print_help()
main()