Files
ess-moxa-configuration-tools/Python/danish_batch_scp.py
2024-11-21 16:07:03 +01:00

119 lines
3.8 KiB
Python

import pandas as pd
import requests
from urllib3.exceptions import InsecureRequestWarning
import json
import scp
import paramiko
import os
from dotenv import load_dotenv
load_dotenv()
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)
default_user = str(os.getenv("DEFAULT_MOXA_SSH_USER"))
default_password = str(os.getenv("DEFAULT_MOXA_SSH_PASSWORD"))
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 = default_user
password = default_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 == "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)