eive-obsw/scripts/q7s-cp.py

113 lines
3.1 KiB
Python
Raw Permalink Normal View History

2021-07-26 01:21:37 +02:00
#!/usr/bin/env python3
import argparse
import os
2022-01-27 17:30:28 +01:00
import sys
2021-07-26 01:21:37 +02:00
def main():
args = handle_args()
cmd = build_cmd(args)
# Run the command
2022-01-27 17:30:28 +01:00
print(f"Running command: {cmd}")
2021-07-28 18:56:36 +02:00
result = os.system(cmd)
if result != 0:
2022-04-29 16:05:40 +02:00
prompt_ssh_key_removal()
2022-01-27 17:30:28 +01:00
print(f'Running command "{cmd}"')
2021-07-28 18:56:36 +02:00
result = os.system(cmd)
2021-07-26 01:21:37 +02:00
2022-04-29 16:05:40 +02:00
def prompt_ssh_key_removal():
2022-11-28 11:21:12 +01:00
do_remove_key = input(
"Do you want to remove problematic keys on localhost ([Y]/n)?: "
)
2022-04-29 16:05:40 +02:00
if not do_remove_key.lower() in ["y", "yes", "1", ""]:
sys.exit(1)
port = 0
while True:
port = input("Enter port to remove: ")
if not port.isdecimal():
print("Invalid port detected")
else:
break
2022-05-23 11:35:55 +02:00
cmd = f'ssh-keygen -f "$HOME/.ssh/known_hosts" -R "[localhost]:{port}"'
2022-04-29 16:05:40 +02:00
print(f"Removing problematic SSH key with command {cmd}..")
os.system(cmd)
2021-07-26 01:21:37 +02:00
def handle_args():
2022-01-27 17:30:28 +01:00
help_string = (
"This script copies files to the Q7S as long as port forwarding is active.\n"
)
help_string += (
"You can set up port forwarding with "
2021-07-26 01:21:37 +02:00
'"ssh -L 1535:192.168.133.10:22 <eive-flatsat-ip>" -t /bin/bash'
)
2022-01-27 17:30:28 +01:00
parser = argparse.ArgumentParser(description=help_string)
2021-07-26 01:21:37 +02:00
# Optional arguments
2022-01-27 17:30:28 +01:00
parser.add_argument(
"-r", "--recursive", dest="recursive", default=False, action="store_true"
)
parser.add_argument(
"-t",
"--target",
help="Target destination. If files are copied to Q7S, will be /tmp by default. "
"If files are copied back to host, will be current directory by default",
default="",
)
parser.add_argument("-P", "--port", help="Target port", default=1535)
parser.add_argument(
"-i",
"--invert",
default=False,
action="store_true",
help="Copy from Q7S to host instead. Always copies to current directory.",
)
2022-02-08 14:59:33 +01:00
parser.add_argument(
"-f",
"--flatsat",
default=False,
action="store_true",
2022-11-28 11:21:12 +01:00
help="Copy to flatsat instead",
2022-02-08 14:59:33 +01:00
)
2021-07-26 01:21:37 +02:00
# Positional argument(s)
2022-01-27 17:30:28 +01:00
parser.add_argument(
2022-04-29 16:05:40 +02:00
"source", help="Source files to copy or target files to copy back to host"
2022-01-27 17:30:28 +01:00
)
2021-07-26 01:21:37 +02:00
return parser.parse_args()
def build_cmd(args):
# Build run command
2022-01-27 17:30:28 +01:00
cmd = "scp "
2021-07-26 01:21:37 +02:00
if args.recursive:
2022-01-27 17:30:28 +01:00
cmd += "-r "
2022-02-08 15:09:46 +01:00
address = ""
port_args = ""
target = args.target
2022-02-08 14:59:33 +01:00
if args.flatsat:
2022-02-08 15:09:46 +01:00
address = "eive@flatsat.eive.absatvirt.lw"
2022-01-27 17:30:28 +01:00
else:
2022-02-08 15:09:46 +01:00
address = "root@localhost"
2022-11-28 11:21:12 +01:00
port_args = f"-P {args.port}"
2022-02-08 15:09:46 +01:00
if args.invert:
if target == "":
2022-02-08 14:59:33 +01:00
target = "."
2022-02-08 15:09:46 +01:00
else:
target = args.target
else:
if target == "":
2022-02-08 14:59:33 +01:00
target = f"/tmp"
else:
2022-02-08 15:09:46 +01:00
target = args.target
# accepted_key_rsa_args = "-o HostKeyAlgorithms=+ssh-rsa -o PubkeyAcceptedKeyTypes=+ssh-rsa"
accepted_key_rsa_args = ""
2022-02-08 15:09:46 +01:00
if args.invert:
2022-09-26 16:20:53 +02:00
cmd += f"{port_args} {accepted_key_rsa_args} {address}:{args.source} {target}"
2022-02-08 15:09:46 +01:00
else:
2022-09-26 16:20:53 +02:00
cmd += f"{port_args} {accepted_key_rsa_args} {args.source} {address}:{target}"
2022-02-08 15:09:46 +01:00
return cmd
2021-07-26 01:21:37 +02:00
if __name__ == "__main__":
main()