#!/usr/bin/python3 """ Establish an SSH connection (key-based) to the router, briefly disable gpsd (wwanmd toggles the port from down to up, leaving it briefly free), and within that window call set_cfg_rate_neom8l.py with the desired rate_hz value, and then re-enable gpsd. Calling the subroutine: python3 run_set_rate.py """ import paramiko import subprocess import time import sys HOST = "192.168.1.1" # The router's IP address as seen from the container PORT = 22 USER = "root" SSH_KEY_PATH = "/root/.ssh/id_rsa" # Path to the private SSH key RETRY_COUNT = 2 # attempts RETRY_DELAY = 0.5 # Seconds between attempts def run_remote2command(client, cmd): stdin, stdout, stderr = client.exec_command(cmd) out = stdout.read().decode().strip() err = stderr.read().decode().strip() status = stdout.channel.recv_exit_status() return status, out, err def main(): if len(sys.argv) != 2: print(f"Usage: {sys.argv[0]} (allowed: 1-10)") sys.exit(1) try: rate_hz = int(sys.argv[1]) if not (1 <= rate_hz <= 10): raise ValueError except ValueError: print("Error: rate_hz must be an integer between 1 and 10.") sys.exit(1) # Establish an SSH Connection to the Router ''Key-Based'' key = paramiko.RSAKey.from_private_key_file(SSH_KEY_PATH) client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(HOST, port=PORT, username=USER, pkey=key, timeout=10) try: # Disable gpsd -> wwanmd toggles the port; it is open for a short time print("Set gpsd.1.status=0 ...") run_remote_command(client, 'cli set -s gpsd.1.status="0"') success = False for attempt in range(1, RETRY_COUNT + 1): print(f"Experiment {attempt}/{RETRY_COUNT}: call set_cfg_rate_neom8l.py on...") result = subprocess.run( ["python3", "set_cfg_rate_neom8l.py", str(rate_hz)], capture_output=True, text=True, timeout=10 ) print("STDOUT:", result.stdout) if result.stderr: print("STDERR:", result.stderr) if result.returncode == 0 and "SerialException" not in result.stderr: success = True break time.sleep(RETRY_DELAY) if not success: print("Note: set_cfg_rate_neom8l.py failed to run successfully in all attempts.") finally: # Turn gpsd back on print("Set gpsd.0.status=1 ...") run_remote_command(client, 'cli set -s gpsd.1.status="1"') client.close() if __name__ == "__main__": main()