Upgrade to 1.5.2, tooling for VPN API requests

This commit is contained in:
Quentin WEPHRE
2024-07-19 08:59:28 +02:00
parent 5819418f7c
commit 1a383c5302
25 changed files with 2026 additions and 1835 deletions

View File

@@ -1,45 +1,49 @@
from azure.iot.hub import IoTHubRegistryManager
from azure.iot.hub.protocol.models import QuerySpecification
from azure.iot.hub.models import CloudToDeviceMethod, CloudToDeviceMethodResult
import json
module_id = "thingspro-agent"
method_name = "thingspro-api-v1"
payload = '{"method":"GET", "path":"/device/general"}'
# Install the Azure IoT Hub SDK:
# pip install azure-iot-hub
# Authenticate to your Azure account
CONNECTION_STRING = "HostName=IotHub-CUBE-PROD.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey= ..."
if CONNECTION_STRING == "":
print("Provide a connection string for the Iot Hub before running the script!")
exit(13)
registry_manager = IoTHubRegistryManager.from_connection_string(CONNECTION_STRING)
query_spec = QuerySpecification(query="SELECT * FROM devices WHERE tags.site != 'QWE' AND tags.number != '0' AND capabilities.iotEdge = true")
query_result = registry_manager.query_iot_hub(query_spec)
devices = []
for item in query_result.items:
devices.append([int(item.tags['number']), item.tags['deviceId'], item.tags['site']])
ordered_devices = sorted(devices, key = lambda x: (x[2], x[0]))
for i in ordered_devices:
current_device_modules = registry_manager.get_modules(i[1])
for module in current_device_modules:
if module.module_id == module_id:
thingspro_module = module
if thingspro_module:
#print("Found thingspro-agent for " + i[1] + " (" + i[2] + ")")
try:
direct_method = CloudToDeviceMethod(method_name=method_name, payload=json.loads(payload))
response = registry_manager.invoke_device_module_method(device_id=i[1], module_id=module_id, direct_method_request=direct_method)
print(str(i[2]), str(i[0]), str(i[1]), response.payload['data']['description'],sep=";")
except:
print(str(i[2]), str(i[0]), str(i[1]), "UNREACHABLE",sep=";")
else:
from azure.iot.hub import IoTHubRegistryManager
from azure.iot.hub.protocol.models import QuerySpecification
from azure.iot.hub.models import CloudToDeviceMethod, CloudToDeviceMethodResult
import json
module_id = "thingspro-agent"
method_name = "thingspro-api-v1"
payload = '{"method":"GET", "path":"/device/general"}'
# Install the Azure IoT Hub SDK:
# pip install azure-iot-hub
# Authenticate to your Azure account
CONNECTION_STRING = "HostName=IotHub-CUBE-PROD.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=...="
if CONNECTION_STRING == "":
print("Provide a connection string for the Iot Hub before running the script!")
exit(13)
registry_manager = IoTHubRegistryManager.from_connection_string(CONNECTION_STRING)
query_spec = QuerySpecification(query="SELECT * FROM devices WHERE tags.site = 'MYRTLE' AND tags.number != '0' AND capabilities.iotEdge = true")
query_result = registry_manager.query_iot_hub(query_spec)
devices = []
for item in query_result.items:
devices.append([int(item.tags['number']), item.tags['deviceId'], item.tags['site']])
ordered_devices = sorted(devices, key = lambda x: (x[2], x[0]))
for i in ordered_devices:
current_device_modules = registry_manager.get_modules(i[1])
for module in current_device_modules:
if module.module_id == module_id:
thingspro_module = module
if thingspro_module:
#print("Found thingspro-agent for " + i[1] + " (" + i[2] + ")")
try:
direct_method = CloudToDeviceMethod(method_name=method_name, payload=json.loads(payload))
response = registry_manager.invoke_device_module_method(device_id=i[1], module_id=module_id, direct_method_request=direct_method)
#print(response.payload)
if str(i[1]) == str(response.payload['data']['hostName']):
print(str(i[2]), str(i[0]), str(i[1]), response.payload['data']['description'], response.payload['data']['firmwareVersion'], sep=";")
else:
print(str(i[2]), str(i[0]), str(i[1]), response.payload['data']['description'], response.payload['data']['hostName'], response.payload['data']['firmwareVersion'], sep=";")
except:
print(str(i[2]), str(i[0]), str(i[1]), "UNREACHABLE",sep=";")
else:
print("No thingspro-agent available for " + i[1] + " (" + i[2] + ")")

View File

@@ -1,144 +1,144 @@
import datetime
import time
import pandas as pd
import requests
from urllib3.exceptions import InsecureRequestWarning
import jq
import json
# Function to authenticate and get token
def authenticate(device_ip, payload):
auth_url = f"https://{device_ip}:8443/api/v1/auth"
response = requests.post(auth_url, json=payload, verify=False)
if response.status_code == 200:
token = response.json()["data"]["token"]
#print(f"Authentication successful. Token received: {token}")
print(" authenticated!")
return token
else:
print(f"Authentication failed. Status code: {response.status_code}")
return None
# Function to send PATCH request
def send_patch_request(device_ip, token, connection_string):
headers = {
"mx-api-token": token
}
payload = {
"provisioning": {
"source": "manual",
"connectionString": connection_string,
"enable": True
}
}
patch_url = f"https://{device_ip}:8443/api/v1/azure-iotedge"
response = requests.patch(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
print(f"PATCH request successful for device {device_ip}")
else:
print(f"Failed to send PATCH request to device {device_ip}. Status code: {response.status_code}")
# Function to send UPGRADE request
def send_upgrade_request(device_ip, token, upgrade_url):
headers = {
"mx-api-token": token
}
payload = {
"deleteFileAfterInstallComplete": True,
"install": True,
"url": upgrade_url
}
patch_url = f"https://{device_ip}:8443/api/v1/upgrades"
response = requests.post(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
print(f"POST request successful for device {device_ip}")
else:
print(f"Failed to send POST request to device {device_ip}. Status code: {response.status_code}")
print(response.content)
# Function to send UPGRADE request
def get_upgrade_jobs(device_ip, token):
headers = {
"mx-api-token": token
}
payload = {
}
patch_url = f"https://{device_ip}:8443/api/v1/upgrades/7"
response = requests.get(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
#print(f"GET request successful for device {device_ip}")
json_data = json.loads(response.content.decode())
json_str = json.dumps(json_data, indent=4)
#print(json_str)
print(jq.compile('.data.tasks[0].progress').input(json.loads(json_str)).first(), end="% speed: ")
print(jq.compile('.data.tasks[0].speed').input(json.loads(json_str)).first())
else:
print(f"Failed to send GET request to device {device_ip}. Status code: {response.status_code}")
print(response.content)
# Function to send UPGRADE request
def get_API_1(device_ip, token):
headers = {
"mx-api-token": token
}
payload = {
}
patch_url = f"https://{device_ip}:8443/api/v1/azure-iotedge/messages"
response = requests.get(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
#print(f"GET request successful for device {device_ip}")
#json_data = json.loads(response.content.decode())
#json_str = json.dumps(json_data)
#print(jq.compile('.data.completedAt').input(json.loads(json_str)).first())
parsed = json.loads(response.content.decode())
print(json.dumps(parsed, indent=4))
else:
print(f"Failed to send GET request to device {device_ip}. Status code: {response.status_code}")
print(response.content.decode())
# Read the Excel file
# df = pd.read_excel("")
# df = df[df["device_name"].notnull()]
# Iterate over each row in the DataFrame
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
payload_auth = {
"acceptEULA": True,
"name": "",
"password": ""
}
if payload_auth["name"] == "" or payload_auth["password"] == "":
print("Provide the credentials before running the script!")
exit(10)
ip_address = "10.197.35.116"
token = authenticate(ip_address, payload_auth)
upgrade_url = ""#"https://files.thingsprocloud.com/package/moxa-aig-301-series-includes-security-patch-firmware-v1.0.deb.yaml"
if upgrade_url == "":
print("Provide upgrade URL before running the script!")
exit(12)
if token:
while True:
print(datetime.datetime.now().strftime("%H:%M:%S"), end=" ")
get_upgrade_jobs(ip_address, token)
time.sleep(60)
# for index, row in df.iterrows():
# device_name = row['device_name']
# device_ip_address_https = row['device_ip_address_http']
# connection_string = row['connection_string']
# upgrade_url = "https://files.thingsprocloud.com/package/moxa-aig-301-series-includes-security-patch-firmware-v1.0.deb.yaml"
# # Authenticate and get token
# payload_auth = {
# "acceptEULA": True,
# "name": "",
# "password": ""
# }
# print(device_name, end="")
# token = authenticate(device_ip_address_https, payload_auth)
# if token:
# get_API(device_ip_address_https, token)
# print("\n")
import datetime
import time
import pandas as pd
import requests
from urllib3.exceptions import InsecureRequestWarning
import jq
import json
# Function to authenticate and get token
def authenticate(device_ip, payload):
auth_url = f"https://{device_ip}:8443/api/v1/auth"
response = requests.post(auth_url, json=payload, verify=False)
if response.status_code == 200:
token = response.json()["data"]["token"]
#print(f"Authentication successful. Token received: {token}")
print(" authenticated!")
return token
else:
print(f"Authentication failed. Status code: {response.status_code}")
return None
# Function to send PATCH request
def send_patch_request(device_ip, token, connection_string):
headers = {
"mx-api-token": token
}
payload = {
"provisioning": {
"source": "manual",
"connectionString": connection_string,
"enable": True
}
}
patch_url = f"https://{device_ip}:8443/api/v1/azure-iotedge"
response = requests.patch(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
print(f"PATCH request successful for device {device_ip}")
else:
print(f"Failed to send PATCH request to device {device_ip}. Status code: {response.status_code}")
# Function to send UPGRADE request
def send_upgrade_request(device_ip, token, upgrade_url):
headers = {
"mx-api-token": token
}
payload = {
"deleteFileAfterInstallComplete": True,
"install": True,
"url": upgrade_url
}
patch_url = f"https://{device_ip}:8443/api/v1/upgrades"
response = requests.post(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
print(f"POST request successful for device {device_ip}")
else:
print(f"Failed to send POST request to device {device_ip}. Status code: {response.status_code}")
print(response.content)
# Function to send UPGRADE request
def get_upgrade_jobs(device_ip, token):
headers = {
"mx-api-token": token
}
payload = {
}
patch_url = f"https://{device_ip}:8443/api/v1/upgrades/7"
response = requests.get(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
#print(f"GET request successful for device {device_ip}")
json_data = json.loads(response.content.decode())
json_str = json.dumps(json_data, indent=4)
#print(json_str)
print(jq.compile('.data.tasks[0].progress').input(json.loads(json_str)).first(), end="% speed: ")
print(jq.compile('.data.tasks[0].speed').input(json.loads(json_str)).first())
else:
print(f"Failed to send GET request to device {device_ip}. Status code: {response.status_code}")
print(response.content)
# Function to send UPGRADE request
def get_API_1(device_ip, token):
headers = {
"mx-api-token": token
}
payload = {
}
patch_url = f"https://{device_ip}:8443/api/v1/azure-iotedge/messages"
response = requests.get(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
#print(f"GET request successful for device {device_ip}")
#json_data = json.loads(response.content.decode())
#json_str = json.dumps(json_data)
#print(jq.compile('.data.completedAt').input(json.loads(json_str)).first())
parsed = json.loads(response.content.decode())
print(json.dumps(parsed, indent=4))
else:
print(f"Failed to send GET request to device {device_ip}. Status code: {response.status_code}")
print(response.content.decode())
# Read the Excel file
# df = pd.read_excel("")
# df = df[df["device_name"].notnull()]
# Iterate over each row in the DataFrame
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
payload_auth = {
"acceptEULA": True,
"name": "",
"password": ""
}
if payload_auth["name"] == "" or payload_auth["password"] == "":
print("Provide the credentials before running the script!")
exit(10)
ip_address = "10.197.35.116"
token = authenticate(ip_address, payload_auth)
upgrade_url = ""#"https://files.thingsprocloud.com/package/moxa-aig-301-series-includes-security-patch-firmware-v1.0.deb.yaml"
if upgrade_url == "":
print("Provide upgrade URL before running the script!")
exit(12)
if token:
while True:
print(datetime.datetime.now().strftime("%H:%M:%S"), end=" ")
get_upgrade_jobs(ip_address, token)
time.sleep(60)
# for index, row in df.iterrows():
# device_name = row['device_name']
# device_ip_address_https = row['device_ip_address_http']
# connection_string = row['connection_string']
# upgrade_url = "https://files.thingsprocloud.com/package/moxa-aig-301-series-includes-security-patch-firmware-v1.0.deb.yaml"
# # Authenticate and get token
# payload_auth = {
# "acceptEULA": True,
# "name": "",
# "password": ""
# }
# print(device_name, end="")
# token = authenticate(device_ip_address_https, payload_auth)
# if token:
# get_API(device_ip_address_https, token)
# print("\n")

View File

@@ -1,147 +1,302 @@
import pandas as pd
import requests
from urllib3.exceptions import InsecureRequestWarning
import jq
import json
# Function to authenticate and get token
def authenticate(device_ip, payload):
auth_url = f"https://{device_ip}:8443/api/v1/auth"
response = requests.post(auth_url, json=payload, verify=False)
if response.status_code == 200:
token = response.json()["data"]["token"]
#print(f"Authentication successful. Token received: {token}")
print(" authenticated!")
return token
else:
print(f"Authentication failed. Status code: {response.status_code}")
return None
# Function to send PATCH request
def send_patch_request(device_ip, token, connection_string):
headers = {
"mx-api-token": token
}
payload = {
"provisioning": {
"source": "manual",
"connectionString": connection_string,
"enable": True
}
}
patch_url = f"https://{device_ip}:8443/api/v1/azure-iotedge"
response = requests.patch(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
print(f"PATCH request successful for device {device_ip}")
else:
print(f"Failed to send PATCH request to device {device_ip}. Status code: {response.status_code}")
# Function to send UPGRADE request
def send_upgrade_request(device_ip, token, upgrade_url):
headers = {
"mx-api-token": token
}
payload = {
"deleteFileAfterInstallComplete": True,
"install": True,
"url": upgrade_url
}
patch_url = f"https://{device_ip}:8443/api/v1/upgrades"
response = requests.post(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
print(f"POST request successful for device {device_ip}")
else:
print(f"Failed to send POST request to device {device_ip}. Status code: {response.status_code}")
print(response.content)
# Function to send UPGRADE request
def get_upgrade_jobs(device_ip, token):
headers = {
"mx-api-token": token
}
payload = {
}
patch_url = f"https://{device_ip}:8443/api/v1/upgrades/2"
response = requests.get(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
#print(f"GET request successful for device {device_ip}")
json_data = json.loads(response.content.decode())
json_str = json.dumps(json_data)
print(jq.compile('.data.completedAt').input(json.loads(json_str)).first())
else:
print(f"Failed to send GET request to device {device_ip}. Status code: {response.status_code}")
print(response.content)
# Function to send UPGRADE request
def put_API(device_ip, token):
headers = {
"mx-api-token": token
}
payload = {
}
patch_url = f"https://{device_ip}:8443/api/v1/azure-iotedge/reset"
response = requests.put(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
#print(f"GET request successful for device {device_ip}")
#json_data = json.loads(response.content.decode())
#json_str = json.dumps(json_data)
#print(jq.compile('.data.completedAt').input(json.loads(json_str)).first())
print(response.content.decode())
else:
print(f"Failed to send GET request to device {device_ip}. Status code: {response.status_code}")
print(response.content.decode())
def get_API(device_ip, token):
headers = {
"mx-api-token": token
}
payload = {
}
patch_url = f"https://{device_ip}:8443/api/v1/azure-iotedge"
response = requests.get(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
#print(f"GET request successful for device {device_ip}")
#json_data = json.loads(response.content.decode())
#json_str = json.dumps(json_data)
#print(jq.compile('.data.completedAt').input(json.loads(json_str)).first())
print(response.content.decode())
else:
print(f"Failed to send GET request to device {device_ip}. Status code: {response.status_code}")
print(response.content.decode())
# Read the Excel file
excel_file_path = ""
if excel_file_path == "":
print("Provide Excel file path before running the script!")
exit(11)
df = pd.read_excel(excel_file_path)
df = df[df["device_name"].notnull()]
# Iterate over each row in the DataFrame
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
for index, row in df.iterrows():
device_name = row['device_name']
device_ip_address_https = row['device_ip_address_http']
connection_string = row['connection_string']
upgrade_url = "" #https://files.thingsprocloud.com/package/moxa-aig-301-series-includes-security-patch-firmware-v1.0.deb.yaml
if upgrade_url == "":
print("Provide upgrade URL before running the script!")
exit(12)
# Authenticate and get token
payload_auth = {
"acceptEULA": True,
"name": "",
"password": ""
}
if payload_auth["name"] == "" or payload_auth["password"] == "":
print("Provide the credentials before running the script!")
exit(10)
print(device_name, end="")
token = authenticate(device_ip_address_https, payload_auth)
if token:
get_API(device_ip_address_https, token)
print("\n")
import pandas as pd
import requests
from urllib3.exceptions import InsecureRequestWarning
import jq
import json
# Function to authenticate and get token
def authenticate(device_ip, payload):
auth_url = f"https://{device_ip}:8443/api/v1/auth"
response = requests.post(auth_url, json=payload, verify=False)
if response.status_code == 200:
token = response.json()["data"]["token"]
#print(f"Authentication successful. Token received: {token}")
#print(" authenticated!")
return token
else:
print(f"Authentication failed. Status code: {response.status_code}")
return None
# Function to send PATCH request
def send_patch_request(device_ip, token, connection_string):
headers = {
"mx-api-token": token
}
payload = {
"provisioning": {
"source": "manual",
"connectionString": connection_string,
"enable": True
}
}
patch_url = f"https://{device_ip}:8443/api/v1/azure-iotedge"
response = requests.patch(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
print(f"PATCH request successful for device {device_ip}")
else:
print(f"Failed to send PATCH request to device {device_ip}. Status code: {response.status_code}")
# Function to send PATCH request
def patch_time(device_ip, token):
headers = {
"mx-api-token": token
}
# payload = {
# "ntp": {
# "source": "timeserver",
# "server": "10.84.171.254",
# "enable": True
# }
# }
payload = {
"timezone": "America/Chicago"
}
patch_url = f"https://{device_ip}:8443/api/v1/device/time"
response = requests.patch(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
json_data = json.loads(response.content.decode())
time = json_data['data']['time']
timezone = json_data['data']['timezone']
last = json_data['data']['lastUpdateTime']
server = json_data['data']['ntp']['server']
enabled = json_data['data']['ntp']['enable']
print(time + " " + timezone + " " + last + " " + server + " " + str(enabled))
else:
json_data = json.loads(response.content.decode())
#print(json.dumps(json_data, indent=2))
# Function to send UPGRADE request
def send_upgrade_request(device_ip, token, upgrade_url):
headers = {
"mx-api-token": token
}
payload = {
"download": True,
"install": True,
"url": upgrade_url,
}
patch_url = f"https://{device_ip}:8443/api/v1/upgrades"
response = requests.post(patch_url, json=payload, headers=headers, verify=False)
json_data = json.loads(response.content.decode())
id = json_data['data']['id']
return id
# Function to send UPGRADE request
def get_upgrade_job(device_ip, token, id):
headers = {
"mx-api-token": token
}
payload = {
}
patch_url = f"https://{device_ip}:8443/api/v1/upgrades/{id}"
response = requests.get(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
#print(f"GET request successful for device {device_ip}")
#print(json_str)
#print(json_data['data'][json_data['count'] - 1]['parameter']['url'], json_data['data'][json_data['count'] - 1]['state'], json_data['count'])
json_data = json.loads(response.content.decode())
getid = json_data['data']['id']
created = json_data['data']['createdAt']
cur_status = json_data['data']['state']
current_tasks = json_data['data']['completedTask']
total_tasks = json_data['data']['totalTask']
print("JOB #" + str(getid) + " " + str(current_tasks) + "/" + str(total_tasks))
print("CREATED ON: " + str(created))
print("CURRENT STATUS: " + str(cur_status))
print(json.dumps(json_data, indent=2))
else:
print(f"Failed to send GET request to device {device_ip}. Status code: {response.status_code}")
print(response.content)
def get_upgrade_jobs(device_ip, token):
headers = {
"mx-api-token": token
}
payload = {
}
patch_url = f"https://{device_ip}:8443/api/v1/upgrades"
response = requests.get(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
#print(f"GET request successful for device {device_ip}")
#print(json_str)
#print(json_data['data'][json_data['count'] - 1]['parameter']['url'], json_data['data'][json_data['count'] - 1]['state'], json_data['count'])
json_data = json.loads(response.content.decode())
count = json_data['count']
print(str(count))
for i in range(count):
getid = json_data['data'][i]['id']
created = json_data['data'][i]['createdAt']
cur_status = json_data['data'][i]['state']
current_tasks = json_data['data'][i]['completedTask']
total_tasks = json_data['data'][i]['totalTask']
print("JOB #" + str(getid) + " " + str(current_tasks) + "/" + str(total_tasks))
print("CREATED ON: " + str(created))
print("CURRENT STATUS: " + str(cur_status))
else:
print(f"Failed to send GET request to device {device_ip}. Status code: {response.status_code}")
print(response.content)
# Function to send UPGRADE request
def start_upgrade_job(device_ip, token, id):
headers = {
"mx-api-token": token
}
payload = {
}
patch_url = f"https://{device_ip}:8443/api/v1/upgrades/{id}/start"
response = requests.put(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
json_data = json.loads(response.content.decode())
curid = json_data['data']['id']
startedat = json_data['data']['startedAt']
print("Job #" + str(curid) + " started on " + str(startedat))
else:
print(f"Failed to send GET request to device {device_ip}. Status code: {response.status_code}")
print(response.content.decode())
# Function to send UPGRADE request
def put_API(device_ip, token):
headers = {
"mx-api-token": token
}
payload = {
}
patch_url = f"https://{device_ip}:8443/api/v1/upgrades/3/start"
response = requests.put(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
#print(f"GET request successful for device {device_ip}")
#json_data = json.loads(response.content.decode())
#json_str = json.dumps(json_data)
#print(jq.compile('.data.completedAt').input(json.loads(json_str)).first())
json_data = json.loads(response.content.decode())
print(json_data['data']['firmwareVersion'])
else:
print(f"Failed to send GET request to device {device_ip}. Status code: {response.status_code}")
print(response.content.decode())
def get_API(device_ip, token):
headers = {
"mx-api-token": token
}
payload = {
}
patch_url = f"https://{device_ip}:8443/api/v1/device/general"
response = requests.get(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
#print(f"GET request successful for device {device_ip}")
#json_data = json.loads(response.content.decode())
#json_str = json.dumps(json_data)
#print(jq.compile('.data.completedAt').input(json.loads(json_str)).first())
json_data = json.loads(response.content.decode())
hostname = json_data['data']['hostName']
serial = json_data['data']['serialNumber']
version = json_data['data']['firmwareVersion']
description = json_data['data']['description']
print(hostname + " " + serial + " " + version + " " + description)
#json_str = json.dumps(json_data, indent=2)
#print(json_str)
else:
print(f"Failed to send GET request to device {device_ip}. Status code: {response.status_code}")
print(response.content.decode())
def get_time(device_ip, token):
headers = {
"mx-api-token": token
}
payload = {
}
patch_url = f"https://{device_ip}:8443/api/v1/device/time"
response = requests.get(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
#print(f"GET request successful for device {device_ip}")
#json_data = json.loads(response.content.decode())
#json_str = json.dumps(json_data)
#print(jq.compile('.data.completedAt').input(json.loads(json_str)).first())
json_data = json.loads(response.content.decode())
time = json_data['data']['time']
timezone = json_data['data']['timezone']
last = json_data['data']['lastUpdateTime']
print(time + " " + timezone + " " + last)
#json_str = json.dumps(json_data, indent=2)
#print(json_str)
else:
print(f"Failed to send GET request to device {device_ip}. Status code: {response.status_code}")
print(response.content.decode())
def delete_API(device_ip, token):
headers = {
"mx-api-token": token
}
payload = {
}
patch_url = f"https://{device_ip}:8443/api/v1/upgrades/5"
response = requests.delete(patch_url, json=payload, headers=headers, verify=False)
print(response.status_code)
json_data = json.loads(response.content.decode())
json_str = json.dumps(json_data, indent=2)
print(json_str)
# Read the Excel file
# excel_file_path = ""
# if excel_file_path == "":
# print("Provide Excel file path before running the script!")
# exit(11)
# df = pd.read_excel(excel_file_path)
# df = df[df["device_name"].notnull()]
# Iterate over each row in the DataFrame
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
# for index, row in df.iterrows():
# device_name = row['device_name']
# device_ip_address_https = row['device_ip_address_http']
# connection_string = row['connection_string']
# upgrade_url = "" #https://files.thingsprocloud.com/package/moxa-aig-301-series-includes-security-patch-firmware-v1.0.deb.yaml
# if upgrade_url == "":
# print("Provide upgrade URL before running the script!")
# exit(12)
# # Authenticate and get token
# payload_auth = {
# "acceptEULA": True,
# "name": "",
# "password": ""
# }
# if payload_auth["name"] == "" or payload_auth["password"] == "":
# print("Provide the credentials before running the script!")
# exit(10)
# print(device_name, end="")
# token = authenticate(device_ip_address_https, payload_auth)
# if token:
# get_API(device_ip_address_https, token)
# print("\n")
for i in range(193, 222):
upgrade_url = "https://files.thingsprocloud.com/package/Upgrade_AIG-301_2.4.0-4020_IMG_1.4_to_1.5.deb.yaml"
payload_auth = {
"acceptEULA": True,
"name": "admin",
"password": "admin@123"
}
device_ip_address = str("10.84.171." + str(i))
#print(device_ip_address, end="")
token = authenticate(device_ip_address, payload_auth)
if token:
#id = send_upgrade_request(device_ip_address,token,upgrade_url)
#print(id)
#get_upgrade_job(device_ip_address, token, 6)
#start_upgrade_job(device_ip_address, token, id)
#put_API(device_ip_address, token)
#patch_time(device_ip_address,token)
#get_time(device_ip_address, token)
#delete_API(device_ip_address,token)
get_API(device_ip_address, token)
else:
print("Authentication failed!")

View File

@@ -1,104 +1,112 @@
import pandas as pd
import requests
from urllib3.exceptions import InsecureRequestWarning
import jq
import json
import scp
import paramiko
def scp_file(local_path, remote_path, hostname, username, password):
try:
# Create a new SSH client
ssh_client = paramiko.SSHClient()
# Automatically add the server's host key
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect to the server
ssh_client.connect(hostname, username=username, password=password)
# Use SCP to transfer the file
with scp.SCPClient(ssh_client.get_transport()) as scp_client:
scp_client.put(local_path, remote_path)
print("File transferred successfully")
except Exception as e:
print(f"Error: {e}")
finally:
# Close the SSH connection
ssh_client.close()
def ssh_execute_command_with_password(hostname, username, password, command):
try:
# Create a new SSH client
ssh_client = paramiko.SSHClient()
# Automatically add the server's host key
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect to the server
ssh_client.connect(hostname, username=username, password=password)
# Execute the command with sudo -S to read password from stdin
stdin, stdout, stderr = ssh_client.exec_command('sudo -k -S ' + command)
stdin.write(password + '\n')
stdin.flush()
# Read the output
output = stdout.read().decode('utf-8')
error = stderr.read().decode('utf-8')
# Print output and errors, if any
if output:
print("Command output:")
print(output)
if error:
print("Command error:")
print(error)
print("Command executed successfully")
except Exception as e:
print(f"Error: {e}")
finally:
# Close the SSH connection
ssh_client.close()
# Read the Excel file
excel_file_path = ""
if excel_file_path == "":
print("Provide Excel file path before running the script!")
exit(11)
df = pd.read_excel(excel_file_path)
df = df[df["device_name"].notnull()]
# Iterate over each row in the DataFrame
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
local_file_path = ""#"./azureiotedge_2.4.0-2697_armhf.mpkg"
if local_file_path == "":
print("Provide upgrade file path before running the script!")
exit(12)
remote_file_path = "./."
username = ""
password = ""
if username == "" or password == "":
print("Provide credentials before running the script!")
exit(10)
command = ""#"appman app install azureiotedge_2.4.0-2697_armhf.mpkg"
if command == "":
print("Provide a command to execute before running the script!")
exit(11)
for index, row in df.iterrows():
device_name = row['device_name']
device_ip_address_https = row['device_ip_address_http']
print(device_name)
ssh_execute_command_with_password(device_ip_address_https, username, password, command)
print("\n")
import pandas as pd
import requests
from urllib3.exceptions import InsecureRequestWarning
import json
import scp
import paramiko
def scp_file(local_path, remote_path, hostname, username, password):
try:
# Create a new SSH client
ssh_client = paramiko.SSHClient()
# Automatically add the server's host key
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect to the server
ssh_client.connect(hostname, username=username, password=password)
# Use SCP to transfer the file
with scp.SCPClient(ssh_client.get_transport()) as scp_client:
scp_client.put(local_path, remote_path)
print("File transferred successfully")
except Exception as e:
print(f"Error: {e}")
finally:
# Close the SSH connection
ssh_client.close()
def ssh_execute_command_with_password(hostname, username, password, command):
try:
# Create a new SSH client
ssh_client = paramiko.SSHClient()
# Automatically add the server's host key
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect to the server
ssh_client.connect(hostname, username=username, password=password)
# Execute the command with sudo -S to read password from stdin
stdin, stdout, stderr = ssh_client.exec_command('sudo -k -S ' + command)
stdin.write(password + '\n')
stdin.flush()
# Read the output
output = stdout.read().decode('utf-8')
error = stderr.read().decode('utf-8')
# Print output and errors, if any
if output:
print("Command output:")
print(output)
if error:
print("Command error:")
print(error)
print("Command executed successfully")
except Exception as e:
print(f"Error: {e}")
finally:
# Close the SSH connection
ssh_client.close()
# Read the Excel file
# excel_file_path = ""
# if excel_file_path == "":
# print("Provide Excel file path before running the script!")
# exit(11)
# df = pd.read_excel(excel_file_path)
# df = df[df["device_name"].notnull()]
# Iterate over each row in the DataFrame
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
local_file_path = "AIG-301_1.5.2-20240625_saft1_armhf.deb"
if local_file_path == "":
print("Provide upgrade file path before running the script!")
exit(12)
remote_file_path = "./."
username = "moxa"
password = "moxa"
if username == "" or password == "":
print("Provide credentials before running the script!")
exit(10)
command = ""#"appman app install azureiotedge_2.4.0-2697_armhf.mpkg"
if command == "dpkg -i AIG-301_1.5.2-20240625_saft1_armhf.deb":
print("Provide a command to execute before running the script!")
exit(11)
# for index, row in df.iterrows():
# device_name = row['device_name']
# device_ip_address_https = row['device_ip_address_http']
# print(device_name)
# #ssh_execute_command_with_password(device_ip_address_https, username, password, command)
# print("\n")
for i in range(131, 160):
device_ip_address = str("10.84.157." + str(i))
print(device_ip_address, end="")
if i == 136 or i == 138 or i == 151:
print(" DONE")
else:
print(" TODO")
scp_file(local_file_path, remote_file_path, device_ip_address, username, password)

View File

@@ -1,87 +1,87 @@
import paramiko
import pandas as pd
import getpass
import re
import time
import json
def read_excel(filename, column_name):
df = pd.read_excel(filename)
df = df[df["device_name"].notnull()]
global names
global ips
global connections
names = df["device_name"].tolist()
ips = df["device_ip_address_http"].tolist()
connections = df["connection_string"].tolist()
return ips
def ssh_execute_commands(device_ip, username, password, commands, connection):
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(device_ip, username=username, password=password)
transport = ssh_client.get_transport()
session = transport.open_session()
session.set_combine_stderr(True)
session.get_pty()
session.exec_command("sudo cat /var/thingspro/data/mx-api-token")
stdin = session.makefile('wb', -1)
stdout = session.makefile('rb', -1)
stdin.write(password + '\n')
stdin.flush()
for line in stdout.read().splitlines():
if not re.search('[Pp]assword', line.decode()):
token = line.decode()
# session = transport.open_session()
# session.set_combine_stderr(True)
# session.get_pty()
# session.exec_command('curl -k -X PUT https://127.0.0.1:8443/api/v1/azure-iotedge/reset -H "Content-Type: application/json" -H "mx-api-token: ' + token + '"')
# stdout = session.makefile('rb', -1)
# for line in stdout.read().splitlines():
# print(line.decode())
print("\n" + connection + "\n")
print(token + "\n")
jq_data = {}
jq_data_2 = {}
jq_data_2["source"] = "manual"
jq_data_2["connectionString"] = connection
jq_data_2["enable"] = True
jq_data["provisioning"] = jq_data_2
json_object = json.dumps(jq_data)
print(json_object + "\n")
session = transport.open_session()
session.set_combine_stderr(True)
session.get_pty()
# -H "Content-Type: application/json" -H "mx-api-token:$(token)'
session.exec_command('curl -k -X PATCH https://127.0.0.1:8443/api/v1/azure-iotedge -H "Content-Type: application/json" -H "mx-api-token: ' + token + '" -d "' + str(json_object) + '"')
stdout = session.makefile('rb', -1)
for line in stdout.read().splitlines():
print(line.decode())
def main():
filename = ""
if filename == "":
print("Provide Excel file path before running the script!")
exit(11)
column_name = "device_ip_address_http"
global names
global ips
global connections
devices = read_excel(filename, column_name)
username = input("Enter SSH username: ")
password = input("Enter SSH password: ")
print(names)
commands = ["sudo bash"] # Add your commands here
for i, device in enumerate(names):
print(f"Connecting to gateway #{i} {device} ({ips[i]})...")
ssh_execute_commands(ips[i], username, password, commands, connections[i])
if __name__ == "__main__":
names = []
ips = []
main()
import paramiko
import pandas as pd
import getpass
import re
import time
import json
def read_excel(filename, column_name):
df = pd.read_excel(filename)
df = df[df["device_name"].notnull()]
global names
global ips
global connections
names = df["device_name"].tolist()
ips = df["device_ip_address_http"].tolist()
connections = df["connection_string"].tolist()
return ips
def ssh_execute_commands(device_ip, username, password, commands, connection):
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(device_ip, username=username, password=password)
transport = ssh_client.get_transport()
session = transport.open_session()
session.set_combine_stderr(True)
session.get_pty()
session.exec_command("sudo cat /var/thingspro/data/mx-api-token")
stdin = session.makefile('wb', -1)
stdout = session.makefile('rb', -1)
stdin.write(password + '\n')
stdin.flush()
for line in stdout.read().splitlines():
if not re.search('[Pp]assword', line.decode()):
token = line.decode()
# session = transport.open_session()
# session.set_combine_stderr(True)
# session.get_pty()
# session.exec_command('curl -k -X PUT https://127.0.0.1:8443/api/v1/azure-iotedge/reset -H "Content-Type: application/json" -H "mx-api-token: ' + token + '"')
# stdout = session.makefile('rb', -1)
# for line in stdout.read().splitlines():
# print(line.decode())
print("\n" + connection + "\n")
print(token + "\n")
jq_data = {}
jq_data_2 = {}
jq_data_2["source"] = "manual"
jq_data_2["connectionString"] = connection
jq_data_2["enable"] = True
jq_data["provisioning"] = jq_data_2
json_object = json.dumps(jq_data)
print(json_object + "\n")
session = transport.open_session()
session.set_combine_stderr(True)
session.get_pty()
# -H "Content-Type: application/json" -H "mx-api-token:$(token)'
session.exec_command('curl -k -X PATCH https://127.0.0.1:8443/api/v1/azure-iotedge -H "Content-Type: application/json" -H "mx-api-token: ' + token + '" -d "' + str(json_object) + '"')
stdout = session.makefile('rb', -1)
for line in stdout.read().splitlines():
print(line.decode())
def main():
filename = ""
if filename == "":
print("Provide Excel file path before running the script!")
exit(11)
column_name = "device_ip_address_http"
global names
global ips
global connections
devices = read_excel(filename, column_name)
username = input("Enter SSH username: ")
password = input("Enter SSH password: ")
print(names)
commands = ["sudo bash"] # Add your commands here
for i, device in enumerate(names):
print(f"Connecting to gateway #{i} {device} ({ips[i]})...")
ssh_execute_commands(ips[i], username, password, commands, connections[i])
if __name__ == "__main__":
names = []
ips = []
main()

View File

@@ -1,83 +1,83 @@
#!/usr/bin/env python3
import asyncio
import logging
import server_async
import time
import math
from pymodbus.payload import BinaryPayloadBuilder
from pymodbus.constants import Endian
from pymodbus.datastore import (
ModbusSequentialDataBlock,
ModbusServerContext,
ModbusSlaveContext,
)
_logger = logging.getLogger(__name__)
async def updating_task(context):
# parameters
fc_as_hex = 3
slave_id = 0x00
address = 0x00
count = 100
# initialization: set values to zero
values = context[slave_id].getValues(fc_as_hex, address, count=count)
values = [0 for v in values]
# infinite loop updating every register to its sine value (period 1 hour, amplitude according to register number)
while True:
await asyncio.sleep(1)
current_time = time.time()
sine_value = sin_wave(current_time)
# BinaryPayloadBuilder is used to convert a float to two Modbus registers
builder = BinaryPayloadBuilder(byteorder=Endian.LITTLE)
for i in range(address, count):
builder.add_32bit_float((i+1) * sine_value)
payload = builder.to_registers()
# once the registers of all the sine waves are created, update the values in the simulator
context[slave_id].setValues(fc_as_hex, address, payload)
# sin wave value calculator
period = 3600
def sin_wave(time_elapsed):
return math.sin(2 * math.pi * time_elapsed / period)
# server start setup function
def setup_updating_server(cmdline=None):
# define the registers
num_floats = 100
float_values = [float(i + 0.5) for i in range(num_floats)]
builder = BinaryPayloadBuilder(byteorder=Endian.LITTLE)
for value in float_values:
builder.add_32bit_float(value)
payload = builder.to_registers()
# create a Modbus simulator with the previously generated registers
datablock = ModbusSequentialDataBlock(0x01, payload)
context = ModbusSlaveContext(di=datablock, co=datablock, hr=datablock, ir=datablock)
context = ModbusServerContext(slaves=context, single=True)
return server_async.setup_server(
description="Run asynchronous server.", context=context, cmdline=cmdline
)
# asynchronous function that will call the values updating function with the arguments (modbus registers layout) of the created server
async def run_updating_server(args):
task = asyncio.create_task(updating_task(args.context))
await server_async.run_async_server(args) # start the server
task.cancel()
# main function that will setup the server, start it and run the function what will update the values of the registers
async def main(cmdline=None):
run_args = setup_updating_server(cmdline=cmdline)
await run_updating_server(run_args)
if __name__ == "__main__":
asyncio.run(main(), debug=True)
#!/usr/bin/env python3
import asyncio
import logging
import server_async
import time
import math
from pymodbus.payload import BinaryPayloadBuilder
from pymodbus.constants import Endian
from pymodbus.datastore import (
ModbusSequentialDataBlock,
ModbusServerContext,
ModbusSlaveContext,
)
_logger = logging.getLogger(__name__)
async def updating_task(context):
# parameters
fc_as_hex = 3
slave_id = 0x00
address = 0x00
count = 100
# initialization: set values to zero
values = context[slave_id].getValues(fc_as_hex, address, count=count)
values = [0 for v in values]
# infinite loop updating every register to its sine value (period 1 hour, amplitude according to register number)
while True:
await asyncio.sleep(1)
current_time = time.time()
sine_value = sin_wave(current_time)
# BinaryPayloadBuilder is used to convert a float to two Modbus registers
builder = BinaryPayloadBuilder(byteorder=Endian.LITTLE)
for i in range(address, count):
builder.add_32bit_float((i+1) * sine_value)
payload = builder.to_registers()
# once the registers of all the sine waves are created, update the values in the simulator
context[slave_id].setValues(fc_as_hex, address, payload)
# sin wave value calculator
period = 3600
def sin_wave(time_elapsed):
return math.sin(2 * math.pi * time_elapsed / period)
# server start setup function
def setup_updating_server(cmdline=None):
# define the registers
num_floats = 100
float_values = [float(i + 0.5) for i in range(num_floats)]
builder = BinaryPayloadBuilder(byteorder=Endian.LITTLE)
for value in float_values:
builder.add_32bit_float(value)
payload = builder.to_registers()
# create a Modbus simulator with the previously generated registers
datablock = ModbusSequentialDataBlock(0x01, payload)
context = ModbusSlaveContext(di=datablock, co=datablock, hr=datablock, ir=datablock)
context = ModbusServerContext(slaves=context, single=True)
return server_async.setup_server(
description="Run asynchronous server.", context=context, cmdline=cmdline
)
# asynchronous function that will call the values updating function with the arguments (modbus registers layout) of the created server
async def run_updating_server(args):
task = asyncio.create_task(updating_task(args.context))
await server_async.run_async_server(args) # start the server
task.cancel()
# main function that will setup the server, start it and run the function what will update the values of the registers
async def main(cmdline=None):
run_args = setup_updating_server(cmdline=cmdline)
await run_updating_server(run_args)
if __name__ == "__main__":
asyncio.run(main(), debug=True)

View File

@@ -1,77 +1,77 @@
import os
import json
import pandas as pd
import pprint
import matplotlib.pyplot as plt
from datetime import datetime
import numpy as np
def process_folder(folder_path, table):
"""
Recursively process each file in the folder and its subfolders.
"""
for root, dirs, files in os.walk(folder_path):
for file in files:
file_path = os.path.join(root, file)
process_file(file_path, table)
def process_file(file_path, table):
"""
Read each line of JSON data from the file and store it in the table.
"""
with open(file_path, 'r') as f:
for line in f:
try:
json_data = json.loads(line)
table.append(json_data)
except json.JSONDecodeError:
print(f"Error decoding JSON in file: {file_path}")
def main(folder_path):
table = []
process_folder(folder_path, table)
# Convert table to pandas DataFrame for easy manipulation
df = pd.DataFrame(table)
index_table = []
enqueued_table = []
emission_table = []
for index, row in df.iterrows():
print(index, end=' ')
index_table.append(index)
print(row['EnqueuedTimeUtc'], end=' ')
enqueued_table.append(row['EnqueuedTimeUtc'])
body_table = []
body_table.append(row['Body'])
body_df = pd.DataFrame(body_table)
for body_index, body_row in body_df.iterrows():
print(body_row['emissionDate'])
emission_table.append(body_row['emissionDate'])
emission_dates = [datetime.strptime(date, '%Y-%m-%dT%H:%M:%SZ') for date in emission_table]
enqueued_dates = [datetime.strptime(date[:19] + date[-1], '%Y-%m-%dT%H:%M:%SZ') for date in enqueued_table]
plt.figure(figsize=(10, 6))
plt.plot(enqueued_dates, index_table, label='Enqueued')
plt.plot(emission_dates, index_table, label='Emission')
plt.xlabel('Time')
plt.ylabel('Index')
plt.title('Index vs Time')
plt.legend()
plt.grid(True)
plt.xticks(rotation=45)
parts = folder_path.split('/')[-4:]
result = '_'.join(parts)
figurename = "index_" + result + ".png"
plt.savefig(figurename, bbox_inches='tight')
if __name__ == "__main__":
folder_path = '/mnt/c/Users/QWPHR/Downloads/JSON_BUFFER_7'
main(folder_path)
import os
import json
import pandas as pd
import pprint
import matplotlib.pyplot as plt
from datetime import datetime
import numpy as np
def process_folder(folder_path, table):
"""
Recursively process each file in the folder and its subfolders.
"""
for root, dirs, files in os.walk(folder_path):
for file in files:
file_path = os.path.join(root, file)
process_file(file_path, table)
def process_file(file_path, table):
"""
Read each line of JSON data from the file and store it in the table.
"""
with open(file_path, 'r') as f:
for line in f:
try:
json_data = json.loads(line)
table.append(json_data)
except json.JSONDecodeError:
print(f"Error decoding JSON in file: {file_path}")
def main(folder_path):
table = []
process_folder(folder_path, table)
# Convert table to pandas DataFrame for easy manipulation
df = pd.DataFrame(table)
index_table = []
enqueued_table = []
emission_table = []
for index, row in df.iterrows():
print(index, end=' ')
index_table.append(index)
print(row['EnqueuedTimeUtc'], end=' ')
enqueued_table.append(row['EnqueuedTimeUtc'])
body_table = []
body_table.append(row['Body'])
body_df = pd.DataFrame(body_table)
for body_index, body_row in body_df.iterrows():
print(body_row['emissionDate'])
emission_table.append(body_row['emissionDate'])
emission_dates = [datetime.strptime(date, '%Y-%m-%dT%H:%M:%SZ') for date in emission_table]
enqueued_dates = [datetime.strptime(date[:19] + date[-1], '%Y-%m-%dT%H:%M:%SZ') for date in enqueued_table]
plt.figure(figsize=(10, 6))
plt.plot(enqueued_dates, index_table, label='Enqueued')
plt.plot(emission_dates, index_table, label='Emission')
plt.xlabel('Time')
plt.ylabel('Index')
plt.title('Index vs Time')
plt.legend()
plt.grid(True)
plt.xticks(rotation=45)
parts = folder_path.split('/')[-4:]
result = '_'.join(parts)
figurename = "index_" + result + ".png"
plt.savefig(figurename, bbox_inches='tight')
if __name__ == "__main__":
folder_path = '/mnt/c/Users/QWPHR/Downloads/JSON_BUFFER_7'
main(folder_path)

View File

@@ -0,0 +1,28 @@
import re
# Function to transform each line
def transform_line(line):
# Use regex to extract parts of the line
match = re.match(r'"([^"]+)","([^"]+)","\[(.*)\]"', line)
if match:
id_part = match.group(1)
date_part = match.group(2)
categories_part = match.group(3)
# Remove extra quotes and split the categories
categories = categories_part.replace('""', '"').split(',')
# Swap categories order and join them with a semicolon
transformed_categories = ','.join(categories[::-1])
# Return the transformed line
return f'"{id_part}",{transformed_categories}'
else:
return None
# Open input file and output file
with open('export_hierarchy', 'r') as infile, open('export_hierarchy_transformed', 'w') as outfile:
for line in infile:
transformed_line = transform_line(line.strip())
if transformed_line:
outfile.write(transformed_line + '\n')