#!/usr/bin/env python3 import paramiko import threading import os from pathlib import Path def start_remote_ptp4l(host, port, username, password, interface, log_file_path): """Start ptp4l on the remote system via SSH and log output locally.""" try: # SSH client setup ssh_client = paramiko.SSHClient() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) print(f"Connecting to {host} to start ptp4l...") ssh_client.connect(hostname=host, port=port, username=username, password=password) # Command to start ptp4l command = f"sudo ptp4l -i {interface} -m" print(f"Running command on remote system: {command}") # Execute the command stdin, stdout, stderr = ssh_client.exec_command(command) # Open local log file for writing with open(log_file_path, "w") as log_file: def stream_output(): for line in iter(stdout.readline, ""): print(line, end="") # Print output to console log_file.write(line) # Save output to local log file # Start streaming the output thread = threading.Thread(target=stream_output, daemon=True) thread.start() # Wait for the process to complete exit_status = stdout.channel.recv_exit_status() print(f"ptp4l exited with status {exit_status}") except Exception as e: print(f"An error occurred: {e}") finally: ssh_client.close() def get_remote_system_time(host, port, username, password): try: # Create an SSH client ssh_client = paramiko.SSHClient() # Automatically add the server's host key if not already known ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # Connect to the remote server print(f"Connecting to {host}...") ssh_client.connect(hostname=host, port=port, username=username, password=password) # Execute the command to get system time stdin, stdout, stderr = ssh_client.exec_command("date") system_time = stdout.read().decode().strip() # Print the system time print(f"System time on {host}: {system_time}") except Exception as e: print(f"An error occurred: {e}") finally: # Close the SSH connection ssh_client.close() if __name__ == "__main__": # Get the directory of the current script script_dir = os.path.dirname(os.path.abspath(__file__)) script_dir += '/logs' # Path(script_dir).mkdir(parents=True, exist_ok=True) # Replace these values with your remote computer details remote_host = "192.168.0.10" # IP or hostname of the remote machine ssh_port = 22 # Default SSH port ssh_username = "root" ssh_password = "meshman11s" # Network interface and log file network_interface = "eth0" # Replace with the actual interface on the remote system log_file_path = os.path.join(script_dir, "ptp4l_remote.log") # Log file in the same directory as the script # Get remote system time get_remote_system_time(remote_host, ssh_port, ssh_username, ssh_password) # Start ptp4l on the remote system and log locally start_remote_ptp4l(remote_host, ssh_port, ssh_username, ssh_password, network_interface, log_file_path)