generated from Luis/nextjs-python-web-template
110 lines
2.9 KiB
Python
Executable File
110 lines
2.9 KiB
Python
Executable File
# !/usr/bin/env python3
|
|
import argparse
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
|
|
from . import admin
|
|
|
|
has_argcomplete = True
|
|
try:
|
|
import argcomplete
|
|
except ImportError: # pragma: no cover
|
|
has_argcomplete = False
|
|
|
|
|
|
def ssh(args: argparse.Namespace) -> None:
|
|
if args.json:
|
|
ssh_data = json.load(args.json)
|
|
subprocess.run(
|
|
[
|
|
"nix",
|
|
"shell",
|
|
"nixpkgs#sshpass",
|
|
"-c",
|
|
"torify",
|
|
"sshpass",
|
|
"-p",
|
|
ssh_data.get("password"),
|
|
"ssh",
|
|
"-o",
|
|
"UserKnownHostsFile=/dev/null",
|
|
"-o",
|
|
"StrictHostKeyChecking=no",
|
|
f'root@{ssh_data["address"]}',
|
|
]
|
|
+ args.ssh_args
|
|
)
|
|
elif args.png:
|
|
png_text = subprocess.Popen(
|
|
[
|
|
"nix",
|
|
"shell",
|
|
"nixpkgs#zbar",
|
|
"-c",
|
|
"zbarimg",
|
|
"--quiet",
|
|
"--raw",
|
|
args.png,
|
|
],
|
|
stdout=subprocess.PIPE,
|
|
).stdout.read()
|
|
ssh_data = json.loads(png_text)
|
|
subprocess.run(
|
|
[
|
|
"nix",
|
|
"shell",
|
|
"nixpkgs#sshpass",
|
|
"-c",
|
|
"torify",
|
|
"sshpass",
|
|
"-p",
|
|
ssh_data.get("password"),
|
|
"ssh",
|
|
"-o",
|
|
"UserKnownHostsFile=/dev/null",
|
|
"-o",
|
|
"StrictHostKeyChecking=no",
|
|
f'root@{ssh_data["address"]}',
|
|
]
|
|
+ args.ssh_args
|
|
)
|
|
|
|
|
|
# this will be the entrypoint under /bin/clan (see pyproject.toml config)
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(description="cLAN tool")
|
|
subparsers = parser.add_subparsers()
|
|
|
|
parser_admin = subparsers.add_parser("admin")
|
|
admin.register_parser(parser_admin)
|
|
|
|
parser_ssh = subparsers.add_parser("ssh", help="ssh to a remote machine")
|
|
parser_ssh.add_argument(
|
|
"-j",
|
|
"--json",
|
|
help="specify the json file for ssh data (generated by starting the clan installer",
|
|
)
|
|
parser_ssh.add_argument(
|
|
"-P",
|
|
"--png",
|
|
help="specify the json file for ssh data as the qrcode image (generated by starting the clan installer",
|
|
)
|
|
# TODO pass all args we don't parse into ssh_args, currently it fails if arg starts with -
|
|
parser_ssh.add_argument("ssh_args", nargs="*", default=[])
|
|
parser_ssh.set_defaults(func=ssh)
|
|
|
|
if has_argcomplete:
|
|
argcomplete.autocomplete(parser)
|
|
|
|
if len(sys.argv) == 1:
|
|
parser.print_help()
|
|
|
|
args = parser.parse_args()
|
|
if hasattr(args, "func"):
|
|
args.func(args)
|
|
|
|
|
|
if __name__ == "__main__": # pragma: no cover
|
|
main()
|