Initial commit
This commit is contained in:
104
Python/danish_batch_scp.py
Normal file
104
Python/danish_batch_scp.py
Normal file
@@ -0,0 +1,104 @@
|
||||
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")
|
||||
|
||||
Reference in New Issue
Block a user