import csv import paramiko from cube_activate_ssh import activate_ssh from dotenv import load_dotenv import os import re import sys def resource_path(relative_path): """ Get absolute path to resource, works for dev and for PyInstaller """ try: # PyInstaller creates a temp folder and stores path in _MEIPASS base_path = sys._MEIPASS except Exception: base_path = os.path.abspath(".") return os.path.join(base_path, relative_path) dotenv_path = resource_path('.env') load_dotenv(dotenv_path=dotenv_path) ip_address_prefix = "10.84.195." ssh_command = "hostname" csv_filename = "ANTWERP_01.csv" ENV_SSH = { "DEFAULT_CUBE_LINUX_ADMIN_USER": os.getenv("DEFAULT_CUBE_LINUX_ADMIN_USER"), "DEFAULT_CUBE_LINUX_ADMIN_PASSWORD": os.getenv("DEFAULT_CUBE_LINUX_ADMIN_PASSWORD") } ssh_username = ENV_SSH["DEFAULT_CUBE_LINUX_ADMIN_USER"] ssh_password = ENV_SSH["DEFAULT_CUBE_LINUX_ADMIN_PASSWORD"] def execute_ssh_command(ip, command): client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: client.connect(ip, port=11022, username=ssh_username, password=ssh_password, allow_agent=False, look_for_keys=False) stdin, stdout, stderr = client.exec_command(command) result = stdout.read().decode().lower().strip() return result except Exception as e: print(f"SSH Error: {str(e)}") raise finally: client.close() def update_cloud_config(ip, new_content): client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: client.connect(ip, port=11022, username=ssh_username, password=ssh_password, allow_agent=False, look_for_keys=False) stdin, stdout, stderr = client.exec_command(f'sudo -S bash -c \'cat > /etc/cube/config-azure.properties << EOF\n{new_content}\nEOF\'\n') stdin.write(ssh_password + "\n") stdin.flush() stdoutput = [line for line in stdout] stderroutput = [line for line in stderr] for output in stdoutput: print(output.strip()) except Exception as e: print(f"SSH Error: {str(e)}") raise finally: client.close() def restart_cloudagent(ip): client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: client.connect(ip, port=11022, username=ssh_username, password=ssh_password, allow_agent=False, look_for_keys=False) stdin, stdout, stderr = client.exec_command(f'sudo -S bash -c \'systemctl restart cube-web-cloudagent << EOF\n\nEOF\'\n') stdin.write(ssh_password + "\n") stdin.flush() stdoutput = [line for line in stdout] stderroutput = [line for line in stderr] for output in stdoutput: print(output.strip()) except Exception as e: print(f"SSH Error: {str(e)}") raise finally: client.close() def main(): print("Starting...") #print(f"{ssh_username} {ssh_password}") with open(csv_filename, mode="w", newline="") as file: writer = csv.writer(file) writer.writerow(["Number", "IP address", "Cube ID", "Environment", "Correct configuration"]) numbers = range(129, 139) for i in numbers: ip_address = f"{ip_address_prefix}{i}" print(f"Activating SSH for {ip_address}:", end=" ") try: activate_ssh(ip_address) except Exception as e: print(f"Failed! {e}") writer.writerow([i, ip_address, "UNREACHABLE", "NA", "NA"]) file.flush() continue print("Activated!") print(f"Executing {ssh_command} for {ip_address}:", end=" ") try: cube_id = execute_ssh_command(ip_address, ssh_command) except Exception as e: print(f"Failed! {e}") writer.writerow([i, ip_address, "UNREACHABLE", "NA", "NA"]) file.flush() continue print(cube_id) print(f"Getting configured Connection String") try: connection_string = execute_ssh_command(ip_address, "grep \"connection-string\" /etc/cube/config-azure.properties") if connection_string == "": raise Exception("No Connection String extracted!") iothub_match = re.search(r"hostname\\=(.*?);", connection_string, re.IGNORECASE) iothub = iothub_match.group(1) if iothub_match else None if iothub.lower() == "IotHub-CUBE-PROD.azure-devices.net".lower(): migration = "SAFT" elif iothub.lower() == "iot-ingest-ess-prod.azure-devices.net".lower(): migration = "INOX" else: migration = "NONE" device_id_match = re.search(r"deviceid\\=(.*?);", connection_string, re.IGNORECASE) cloud_cube_id = device_id_match.group(1) if device_id_match else None if cloud_cube_id.lower() == cube_id.lower(): status = "CORRECT" else: status = "INCORRECT" except Exception as e: print(e) migration = "NONE" status = "INCORRECT" try: print(f"restarting cloud agent on {ip_address}") restart_cloudagent(ip_address) except Exception as e: print(e) writer.writerow([i, ip_address, cube_id, migration, status]) file.flush() if __name__ == "__main__": main()