From ea013e4ecab2665acf6a69d549a88d82cbc6e529 Mon Sep 17 00:00:00 2001 From: Quentin WEPHRE Date: Thu, 26 Mar 2026 09:43:17 +0100 Subject: [PATCH] merged list devices (no impact) --- Python/azure_iot_hub_create_devices.py | 51 +++++++++++++++++++----- Python/azure_iot_hub_list_devices.py | 2 +- Python/cube_ssh_batch_restart_service.py | 12 ++++-- 3 files changed, 51 insertions(+), 14 deletions(-) diff --git a/Python/azure_iot_hub_create_devices.py b/Python/azure_iot_hub_create_devices.py index 40d8acd..e89df33 100644 --- a/Python/azure_iot_hub_create_devices.py +++ b/Python/azure_iot_hub_create_devices.py @@ -1,27 +1,35 @@ from azure.iot.hub import IoTHubRegistryManager from azure.iot.hub.models import Twin, TwinProperties from dotenv import load_dotenv -import pandas as pd +import pandas as pd import os -import json load_dotenv() -INPUT_FILE = "cottonwood_devices.xlsx" # Path to your Excel file -SITE_NAME = "COTTONWOOD" # Parameterized site name -VERSION = "1.5.0" # Parameterized version +INPUT_FILE = "HONEYCOMB_DEVICE_LIST.xlsx" # Path to your Excel file +SITE_NAME = "Honeycomb" # Parameterized site name +VERSION = "1.6.0" # Parameterized version # Authenticate to your Azure account -# CONNECTION_STRING = str(os.getenv("CONNECTION_STRING_SAFT_PROD")) CONNECTION_STRING = str(os.getenv("CONNECTION_STRING_INOX_PROD")) -if CONNECTION_STRING == "": +if CONNECTION_STRING == "" or CONNECTION_STRING == "None": print("Provide a connection string for the Iot Hub before running the script!") exit(13) +# Extract HostName from CONNECTION_STRING +host_name = "" +for part in CONNECTION_STRING.split(';'): + if part.startswith('HostName='): + host_name = part.split('=', 1)[1] + break df = pd.read_excel(INPUT_FILE, header=None) +# Ensure the dataframe has at least 2 columns to store the connection strings +if df.shape[1] < 2: + df[1] = "" + registry_manager = IoTHubRegistryManager.from_connection_string(CONNECTION_STRING) def create_device(device_name, number): @@ -34,6 +42,18 @@ def create_device(device_name, number): iot_edge=True ) print(f"Created IoT Edge-enabled device: {device_name}") + except Exception as e: + print(f"Error creating {device_name} (it may already exist): {e}") + try: + device = registry_manager.get_device(device_name) + print(f"Retrieved existing device: {device_name}") + except Exception as e2: + print(f"Error retrieving {device_name}: {e2}") + return None + + try: + primary_key = device.authentication.symmetric_key.primary_key + connection_string = f"HostName={host_name};DeviceId={device_name};SharedAccessKey={primary_key}" # Set tags twin = registry_manager.get_twin(device_name) @@ -44,13 +64,24 @@ def create_device(device_name, number): }) registry_manager.update_twin(device_name, twin_patch, twin.etag) print(f"Updated tags for {device_name}") + + return connection_string except Exception as e: print(f"Error processing {device_name}: {e}") + return None # Loop through the Excel file and process each device for index, row in df.iterrows(): device_name = str(row[0]).strip() - if device_name: - create_device(device_name, index + 1) + if device_name and device_name != 'nan': + conn_str = create_device(device_name, index + 1) + if conn_str: + df.at[index, 1] = conn_str -print("Device provisioning completed.") \ No newline at end of file +# Save the updated dataframe back to the Excel file +try: + df.to_excel(INPUT_FILE, index=False, header=False) + print("Device provisioning completed.") +except PermissionError: + print(f"\n[ERROR] Permission denied: '{INPUT_FILE}'.") + print("Please close the Excel file if it's currently open in another program, and then try again.") diff --git a/Python/azure_iot_hub_list_devices.py b/Python/azure_iot_hub_list_devices.py index 1405c9b..089db15 100644 --- a/Python/azure_iot_hub_list_devices.py +++ b/Python/azure_iot_hub_list_devices.py @@ -61,4 +61,4 @@ if "last_activity_time" in df_sorted.columns: ) -df_sorted.to_excel("iot_devices_20260204.xlsx", index=False) \ No newline at end of file +df_sorted.to_excel("iot_devices_2.xlsx", index=False) diff --git a/Python/cube_ssh_batch_restart_service.py b/Python/cube_ssh_batch_restart_service.py index a7fd9c1..2418273 100644 --- a/Python/cube_ssh_batch_restart_service.py +++ b/Python/cube_ssh_batch_restart_service.py @@ -19,10 +19,10 @@ def resource_path(relative_path): dotenv_path = resource_path('.env') load_dotenv(dotenv_path=dotenv_path) -ip_address_prefix = "10.188.10." +ip_address_prefix = "10.84.195." ssh_command = "hostname" -csv_filename = "HOOHANA_01.csv" +csv_filename = "ANTWERP_01.csv" ENV_SSH = { "DEFAULT_CUBE_LINUX_ADMIN_USER": os.getenv("DEFAULT_CUBE_LINUX_ADMIN_USER"), @@ -91,7 +91,7 @@ def main(): writer = csv.writer(file) writer.writerow(["Number", "IP address", "Cube ID", "Environment", "Correct configuration"]) - numbers = range(133, 157) #133 to 156 + numbers = range(129, 139) for i in numbers: ip_address = f"{ip_address_prefix}{i}" print(f"Activating SSH for {ip_address}:", end=" ") @@ -143,6 +143,12 @@ def main(): 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()