import os import sys import time from pathlib import Path import paramiko HOST = "140.44.44.80" USER = "snarf" PASSWORD = "Katzir476!" def main(): print(f"Connecting to {USER}@{HOST}...") ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(HOST, username=USER, password=PASSWORD, timeout=10) print("Connected successfully via password!") def run_cmd(cmd, sudo=False): if sudo: # Run with sudo using password full_cmd = f"echo '{PASSWORD}' | sudo -S bash -c \"{cmd}\"" else: full_cmd = cmd print(f"\n[RUN] {cmd}") stdin, stdout, stderr = ssh.exec_command(full_cmd) out = stdout.read().decode() err = stderr.read().decode() rc = stdout.channel.recv_exit_status() if out: print(out.strip()) if err and "password" not in err.lower(): print(f"[ERR] {err.strip()}", file=sys.stderr) return rc, out, err # 1. Inspect system run_cmd("whoami; hostname; ip addr show | grep inet; lsb_release -a || cat /etc/os-release") # 2. Setup SSH Keys pub_keys = [] ssh_dir = Path(r"C:\Users\Snarf\.ssh") for key_file in ["id_ed25519_snarfnet.pub", "Devices.pub"]: p = ssh_dir / key_file if p.exists(): pub_keys.append(p.read_text(encoding="utf-8").strip()) print(f"Loaded public key from {p}") if pub_keys: joined_keys = "\n".join(pub_keys) # Ensure ~/.ssh exists and add keys for snarf run_cmd("mkdir -p ~/.ssh && chmod 700 ~/.ssh") # Write to authorized_keys sftp = ssh.open_sftp() with sftp.open(".ssh/authorized_keys", "a") as f: for k in pub_keys: f.write(f"\n{k}\n") sftp.chmod(".ssh/authorized_keys", 0o600) print("Updated ~/.ssh/authorized_keys for snarf") # Also add to /root/.ssh/authorized_keys run_cmd(f"mkdir -p /root/.ssh && chmod 700 /root/.ssh && echo '{joined_keys}' >> /root/.ssh/authorized_keys && chmod 600 /root/.ssh/authorized_keys", sudo=True) print("Updated /root/.ssh/authorized_keys for root") ssh.close() print("Initial connection and SSH key deployment finished!") if __name__ == "__main__": main()