-
Notifications
You must be signed in to change notification settings - Fork 2
/
forwarding.py
49 lines (43 loc) · 1.16 KB
/
forwarding.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
import click
import subprocess
from logger import logging
from ssh import run_ssh_command
from wrapper import check_programs_wrap
@click.command(name='forwarding')
def cmd_forwarding():
"""Enable network forwarding for a usb-attached device"""
check_programs_wrap(['syctl', 'iptables'])
result = subprocess.run([
'sysctl',
'net.ipv4.ip_forward=1',
])
if result.returncode != 0:
logging.fatal(f'Failed to enable ipv4 forward via sysctl')
exit(1)
result = subprocess.run([
'iptables',
'-P',
'FORWARD',
'ACCEPT',
])
if result.returncode != 0:
logging.fatal(f'Failed set iptables rule')
exit(1)
result = subprocess.run([
'iptables',
'-A',
'POSTROUTING',
'-t',
'nat',
'-j',
'MASQUERADE',
'-s',
'172.16.42.0/24',
])
if result.returncode != 0:
logging.fatal(f'Failed set iptables rule')
exit(1)
result = run_ssh_command(cmd=['sudo -S route add default gw 172.16.42.2'])
if result.returncode != 0:
logging.fatal(f'Failed to add gateway over ssh')
exit(1)