import paramiko import time # Node configuration nodes = [ {"hostname": "192.168.0.10", "username": "root", "password": "meshman11s", "interface": "eth0"}, # Node 1 {"hostname": "192.168.0.11", "username": "root", "password": "meshman11s", "interface": "eth0"}, # Node 2 ] def ssh_command(hostname, username, password, command): """Execute an SSH command on a remote host.""" client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: client.connect(hostname, username=username, password=password) # Prepend `sudo -S` to allow password passing full_command = f"echo {password} | sudo -S {command}" stdin, stdout, stderr = client.exec_command(command) return stdout.read().decode(), stderr.read().decode() finally: client.close() def start_ptp4l(node): """Start ptp4l on a node and log output to a file.""" command = f"ptp4l -i {node['interface']} -m > /tmp/ptp4l_{node['interface']}.log 2>&1 &" print(f"Starting ptp4l on {node['hostname']}...") ssh_command(node['hostname'], node['username'], node['password'], command) def get_ptp_logs(node): """Retrieve the ptp4l log from a node.""" command = f"cat /tmp/ptp4l_{node['interface']}.log" stdout, stderr = ssh_command(node['hostname'], node['username'], node['password'], command) return stdout def main(): # Start ptp4l on both nodes for node in nodes: start_ptp4l(node) # Allow time for ptp4l to start and synchronize time.sleep(15) # Fetch ptp4l logs from both nodes logs = [get_ptp_logs(node) for node in nodes] # Analyze logs to identify grandmaster and sync status grandmaster = None slave_info = None for idx, log in enumerate(logs): print(f"Logs from Node {nodes[idx]['hostname']}:\n{log}\n") if "selected as grandmaster" in log: grandmaster = nodes[idx]['hostname'] elif "synchronizing to master" in log: slave_info = {"hostname": nodes[idx]['hostname'], "log": log} if grandmaster: print(f"Grandmaster identified: {grandmaster}") else: print("No grandmaster identified.") if slave_info: print(f"Synchronization information from slave {slave_info['hostname']}:\n{slave_info['log']}") else: print("No synchronization information found from slaves.") if __name__ == "__main__": main()