forwarding.py and ssh.py: use exec.run_(root_)cmd()

This commit is contained in:
InsanePrawn 2022-08-15 06:12:27 +02:00
parent 7fcd68ced9
commit 0465d1035a
2 changed files with 19 additions and 18 deletions

View file

@ -1,6 +1,7 @@
import click import click
import subprocess import logging
from logger import logging
from exec import run_root_cmd
from ssh import run_ssh_command from ssh import run_ssh_command
from wrapper import check_programs_wrap from wrapper import check_programs_wrap
@ -10,25 +11,26 @@ def cmd_forwarding():
"""Enable network forwarding for a usb-attached device""" """Enable network forwarding for a usb-attached device"""
check_programs_wrap(['syctl', 'iptables']) check_programs_wrap(['syctl', 'iptables'])
result = subprocess.run([ logging.info("Enabling ipv4 forwarding with sysctl")
result = run_root_cmd([
'sysctl', 'sysctl',
'net.ipv4.ip_forward=1', 'net.ipv4.ip_forward=1',
]) ])
if result.returncode != 0: if result.returncode != 0:
logging.fatal(f'Failed to enable ipv4 forward via sysctl') click.Abort('Failed to enable ipv4 forward via sysctl')
exit(1)
result = subprocess.run([ logging.info("Enabling ipv4 forwarding with iptables")
result = run_root_cmd([
'iptables', 'iptables',
'-P', '-P',
'FORWARD', 'FORWARD',
'ACCEPT', 'ACCEPT',
]) ])
if result.returncode != 0: if result.returncode != 0:
logging.fatal(f'Failed set iptables rule') click.Abort('Failed set iptables rule')
exit(1)
result = subprocess.run([ logging.info("Enabling ipv4 NATting with iptables")
result = run_root_cmd([
'iptables', 'iptables',
'-A', '-A',
'POSTROUTING', 'POSTROUTING',
@ -40,10 +42,9 @@ def cmd_forwarding():
'172.16.42.0/24', '172.16.42.0/24',
]) ])
if result.returncode != 0: if result.returncode != 0:
logging.fatal(f'Failed set iptables rule') click.Abort('Failed set iptables rule')
exit(1)
logging.info("Setting default route on device via ssh")
result = run_ssh_command(cmd=['sudo -S route add default gw 172.16.42.2']) result = run_ssh_command(cmd=['sudo -S route add default gw 172.16.42.2'])
if result.returncode != 0: if result.returncode != 0:
logging.fatal(f'Failed to add gateway over ssh') click.Abort('Failed to add gateway over ssh')
exit(1)

10
ssh.py
View file

@ -2,11 +2,11 @@ from typing import Optional
import logging import logging
import os import os
import pathlib import pathlib
import subprocess
import click import click
from config import config from config import config
from constants import SSH_COMMON_OPTIONS, SSH_DEFAULT_HOST, SSH_DEFAULT_PORT from constants import SSH_COMMON_OPTIONS, SSH_DEFAULT_HOST, SSH_DEFAULT_PORT
from exec import run_cmd
@click.command(name='ssh') @click.command(name='ssh')
@ -44,7 +44,7 @@ def run_ssh_command(cmd: list[str] = [],
'--', '--',
] + cmd ] + cmd
logging.debug(f"running cmd: {full_cmd}") logging.debug(f"running cmd: {full_cmd}")
return subprocess.run(full_cmd) return run_cmd(full_cmd)
def scp_put_files(src: list[str], dst: str, user: str = None, host: str = SSH_DEFAULT_HOST, port: int = SSH_DEFAULT_PORT): def scp_put_files(src: list[str], dst: str, user: str = None, host: str = SSH_DEFAULT_HOST, port: int = SSH_DEFAULT_PORT):
@ -64,7 +64,7 @@ def scp_put_files(src: list[str], dst: str, user: str = None, host: str = SSH_DE
] ]
logging.info(f"Copying files to {user}@{host}:{dst}:\n{src}") logging.info(f"Copying files to {user}@{host}:{dst}:\n{src}")
logging.debug(f"running cmd: {cmd}") logging.debug(f"running cmd: {cmd}")
return subprocess.run(cmd) return run_cmd(cmd)
def find_ssh_keys(): def find_ssh_keys():
@ -95,7 +95,7 @@ def copy_ssh_keys(root_dir: str, user: str):
create = click.confirm("Do you want me to generate an ssh key for you?", True) create = click.confirm("Do you want me to generate an ssh key for you?", True)
if not create: if not create:
return return
result = subprocess.run([ result = run_cmd([
'ssh-keygen', 'ssh-keygen',
'-f', '-f',
os.path.join(pathlib.Path.home(), '.ssh', 'id_ed25519_kupfer'), os.path.join(pathlib.Path.home(), '.ssh', 'id_ed25519_kupfer'),
@ -106,7 +106,7 @@ def copy_ssh_keys(root_dir: str, user: str):
'-N', '-N',
'', '',
]) ])
if result.returncode != 0: if result.returncode != 0: # type: ignore
logging.fatal("Failed to generate ssh key") logging.fatal("Failed to generate ssh key")
keys = find_ssh_keys() keys = find_ssh_keys()