Merge pull request 'clan-cli ssh: --json can be file or str' (#1258) from lassulus-HEAD into main

This commit is contained in:
clan-bot
2024-04-22 18:27:35 +00:00

View File

@@ -3,6 +3,7 @@ import json
import logging import logging
import socket import socket
import subprocess import subprocess
from pathlib import Path
from ..nix import nix_shell from ..nix import nix_shell
@@ -84,21 +85,24 @@ def is_reachable(host: str) -> bool:
def connect_ssh_from_json(ssh_data: dict[str, str]) -> None: def connect_ssh_from_json(ssh_data: dict[str, str]) -> None:
for address in ssh_data["addrs"]: for address in ssh_data["local_addresses"]:
log.debug(f"Trying to reach host on: {address}") log.debug(f"Trying to reach host on: {address}")
if is_reachable(address): if is_reachable(address):
ssh(host=address, password=ssh_data["pass"]) ssh(host=address, password=ssh_data["password"])
exit(0) exit(0)
else: else:
log.debug(f"Could not reach host on {address}") log.debug(f"Could not reach host on {address}")
log.debug(f'Trying to reach host via torify on {ssh_data["tor"]}') log.debug(f'Trying to reach host via torify on {ssh_data["tor"]}')
ssh(host=ssh_data["tor"], password=ssh_data["pass"], torify=True) ssh(host=ssh_data["tor"], password=ssh_data["password"], torify=True)
def main(args: argparse.Namespace) -> None: def main(args: argparse.Namespace) -> None:
if args.json: if args.json:
with open(args.json) as file: json_file = Path(args.json)
ssh_data = json.load(file) if json_file.is_file():
ssh_data = json.loads(json_file.read_text())
else:
ssh_data = json.loads(args.json)
connect_ssh_from_json(ssh_data) connect_ssh_from_json(ssh_data)
elif args.png: elif args.png:
ssh_data = json.loads(qrcode_scan(args.png)) ssh_data = json.loads(qrcode_scan(args.png))