hostapps/hostapps/run.py
2023-10-30 22:57:39 -05:00

126 lines
2.7 KiB
Python

import os
import pathlib
import socket
import sys
import click
import pyansi
from pydantic import BaseModel
from pydantic import TypeAdapter
kex_algorithms = [
"diffie-hellman-group14-sha1",
"diffie-hellman-group1-sha1",
"diffie-hellman-group-exchange-sha1",
"diffie-hellman-group-exchange-sha256",
"ecdh-sha2-nistp256",
]
ciphers = [
"aes256-cbc",
"aes192-cbc",
"3des-cbc",
"aes128-cbc",
"aes256-ctr",
"aes192-ctr",
"aes128-ctr",
]
macs = [
"hmac-md5",
"hmac-sha1",
"hmac-sha2-256-etm@openssh.com",
"hmac-sha2-512-etm@openssh.com",
"hmac-sha2-256",
"hmac-sha2-512",
]
options = [
("Ciphers", f"{','.join(ciphers)}"),
("ControlMaster", "no"),
("ControlPath", "none"),
("ForwardX11", "no"),
("ForwardX11Trusted", "no"),
("HostKeyAlgorithms", "+ssh-rsa"),
("KexAlgorithms", f"{','.join(kex_algorithms)}"),
("MACs", f"{','.join(macs)}"),
("PubkeyAcceptedKeyTypes", "+ssh-rsa"),
]
class Entry(BaseModel):
name: str
type: str
comment: str
address: str
port: int
username: str
manufacturer: str
model: str
part_number: str
Config = TypeAdapter(dict[str, Entry])
@click.command()
@click.option("--ssh-command", default="ssh")
@click.option("--telnet-command", default="telnet")
@click.option("--config", type=click.Path(exists=True, path_type=pathlib.Path))
def main(
ssh_command: str,
telnet_command: str,
config: pathlib.Path,
):
entry = Entry.model_validate_json(config.read_bytes())
a = pyansi.ANSI()
sys.stdout.write(a.Title(text=f"{entry.name} - {entry.address}"))
sys.stdout.flush()
# if 'SSH_AUTH_SOCK' in os.environ:
# journal.send('Removing SSH_AUTH_SOCK from environment')
# del os.environ['SSH_AUTH_SOCK']
command = []
match entry.type:
case "ssh":
command = [
ssh_command,
"ssh",
# "-v",
"-y",
]
if socket.gethostname() != "pc60929":
command += ["-o", "ProxyJump=jcollie@pc60929"]
command += [
"-i",
"~/.ssh/id_dmacc_rsa",
]
for k, v in options:
command += ["-o", f"{k}={v}"]
command += [
"-o",
f"User={entry.username}",
entry.address,
]
case "telnet":
command = [telnet_command, "telnet"]
if socket.gethostname() != "pc60929":
command = [ssh_command, "ssh", "-t", "ssh://jcollie@pc60929", "telnet"]
command += [entry.address]
os.execlp(*command)
if __name__ == "__main__":
main()