88 lines
3.0 KiB
Python
88 lines
3.0 KiB
Python
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()
|