clan-cli: add ssh subcommand
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
# !/usr/bin/env python3
|
# !/usr/bin/env python3
|
||||||
import argparse
|
import argparse
|
||||||
|
import json
|
||||||
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from . import admin
|
from . import admin
|
||||||
@@ -11,6 +13,64 @@ except ImportError: # pragma: no cover
|
|||||||
has_argcomplete = False
|
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)
|
# this will be the entrypoint under /bin/clan (see pyproject.toml config)
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
parser = argparse.ArgumentParser(description="cLAN tool")
|
parser = argparse.ArgumentParser(description="cLAN tool")
|
||||||
@@ -18,6 +78,22 @@ def main() -> None:
|
|||||||
|
|
||||||
parser_admin = subparsers.add_parser("admin")
|
parser_admin = subparsers.add_parser("admin")
|
||||||
admin.register_parser(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:
|
if has_argcomplete:
|
||||||
argcomplete.autocomplete(parser)
|
argcomplete.autocomplete(parser)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user