Compare commits

...

1 Commits

Author SHA1 Message Date
Quentin WEPHRE
ea013e4eca merged list devices (no impact) 2026-03-26 09:43:17 +01:00
3 changed files with 51 additions and 14 deletions

View File

@@ -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.")
# 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.")

View File

@@ -61,4 +61,4 @@ if "last_activity_time" in df_sorted.columns:
)
df_sorted.to_excel("iot_devices_20260204.xlsx", index=False)
df_sorted.to_excel("iot_devices_2.xlsx", index=False)

View File

@@ -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()