forked from clan/clan-core
clan-cli: add ssh subcommand
This commit is contained in:
parent
291b353d9c
commit
09bc5b06c9
@ -1,5 +1,7 @@
|
||||
# !/usr/bin/env python3
|
||||
import argparse
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
from . import admin
|
||||
@ -11,6 +13,64 @@ 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")
|
||||
@ -18,6 +78,22 @@ def main() -> None:
|
||||
|
||||
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)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user