hostapps/hostapps/run.py

126 lines
2.7 KiB
Python
Raw Permalink Normal View History

2022-05-28 14:14:08 -05:00
import os
2023-10-29 21:33:23 -05:00
import pathlib
2022-05-28 14:14:08 -05:00
import socket
2023-10-29 21:33:23 -05:00
import sys
2022-05-28 14:14:08 -05:00
import click
2023-10-29 21:33:23 -05:00
import pyansi
from pydantic import BaseModel
from pydantic import TypeAdapter
2022-05-28 14:14:08 -05:00
kex_algorithms = [
2023-10-30 22:57:39 -05:00
"diffie-hellman-group14-sha1",
2022-05-28 14:14:08 -05:00
"diffie-hellman-group1-sha1",
"diffie-hellman-group-exchange-sha1",
2023-10-30 22:57:39 -05:00
"diffie-hellman-group-exchange-sha256",
"ecdh-sha2-nistp256",
2022-05-28 14:14:08 -05:00
]
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"),
]
2023-10-29 21:33:23 -05:00
class Entry(BaseModel):
name: str
type: str
comment: str
address: str
port: int
username: str
2023-10-30 22:26:24 -05:00
manufacturer: str
model: str
part_number: str
2023-10-29 21:33:23 -05:00
Config = TypeAdapter(dict[str, Entry])
2022-05-28 14:14:08 -05:00
@click.command()
@click.option("--ssh-command", default="ssh")
@click.option("--telnet-command", default="telnet")
2023-10-29 21:33:23 -05:00
@click.option("--config", type=click.Path(exists=True, path_type=pathlib.Path))
2022-05-28 14:14:08 -05:00
def main(
ssh_command: str,
telnet_command: str,
2023-10-29 21:33:23 -05:00
config: pathlib.Path,
2022-05-28 14:14:08 -05:00
):
2023-10-30 17:17:09 -05:00
entry = Entry.model_validate_json(config.read_bytes())
2023-10-29 21:33:23 -05:00
a = pyansi.ANSI()
sys.stdout.write(a.Title(text=f"{entry.name} - {entry.address}"))
2022-05-28 14:14:08 -05:00
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 = []
2023-10-29 21:33:23 -05:00
match entry.type:
2022-05-28 14:14:08 -05:00
case "ssh":
command = [
ssh_command,
"ssh",
2023-10-30 17:28:49 -05:00
# "-v",
2022-05-28 14:14:08 -05:00
"-y",
]
if socket.gethostname() != "pc60929":
2023-10-29 21:33:23 -05:00
command += ["-o", "ProxyJump=jcollie@pc60929"]
2022-05-28 14:14:08 -05:00
command += [
"-i",
"~/.ssh/id_dmacc_rsa",
]
for k, v in options:
command += ["-o", f"{k}={v}"]
command += [
"-o",
2023-10-29 21:33:23 -05:00
f"User={entry.username}",
entry.address,
2022-05-28 14:14:08 -05:00
]
case "telnet":
command = [telnet_command, "telnet"]
if socket.gethostname() != "pc60929":
command = [ssh_command, "ssh", "-t", "ssh://jcollie@pc60929", "telnet"]
2023-10-29 21:33:23 -05:00
command += [entry.address]
2022-05-28 14:14:08 -05:00
os.execlp(*command)
if __name__ == "__main__":
main()