[WiP] Carling Cloud connectivity
This commit is contained in:
159
Python/cube_ssh_set_proxy.py
Normal file
159
Python/cube_ssh_set_proxy.py
Normal file
@@ -0,0 +1,159 @@
|
||||
from paramiko import SSHClient, AutoAddPolicy
|
||||
import paramiko
|
||||
from cube_activate_ssh import activate_ssh
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
import sys
|
||||
import shlex
|
||||
from scp import SCPClient
|
||||
import time
|
||||
|
||||
def resource_path(relative_path):
|
||||
""" Get absolute path to resource, works for dev and for PyInstaller """
|
||||
try:
|
||||
# PyInstaller creates a temp folder and stores path in _MEIPASS
|
||||
base_path = sys._MEIPASS
|
||||
except Exception:
|
||||
base_path = os.path.abspath(".")
|
||||
|
||||
return os.path.join(base_path, relative_path)
|
||||
|
||||
dotenv_path = resource_path('.env')
|
||||
load_dotenv(dotenv_path=dotenv_path)
|
||||
|
||||
ip_address_prefix = "10.81.35." # Carling subnet
|
||||
ip_address_range = list(range(65, 75)) # From 65 to 74
|
||||
ip_address_range.append(85) # Add 85 after 74.
|
||||
|
||||
ENV_SSH = {
|
||||
"DEFAULT_CUBE_LINUX_ADMIN_USER": os.getenv("DEFAULT_CUBE_LINUX_ADMIN_USER"),
|
||||
"DEFAULT_CUBE_LINUX_ADMIN_PASSWORD": os.getenv("DEFAULT_CUBE_LINUX_ADMIN_PASSWORD")
|
||||
}
|
||||
|
||||
ssh_username = ENV_SSH["DEFAULT_CUBE_LINUX_ADMIN_USER"]
|
||||
ssh_password = ENV_SSH["DEFAULT_CUBE_LINUX_ADMIN_PASSWORD"]
|
||||
|
||||
def execute_ssh_command(ip, command, client):
|
||||
try:
|
||||
stdin, stdout, stderr = client.exec_command(command, timeout=180)
|
||||
exit_status = stdout.channel.recv_exit_status()
|
||||
|
||||
result = stdout.read().decode().lower().strip()
|
||||
error = stderr.read().decode('utf-8')
|
||||
|
||||
if exit_status == 0:
|
||||
print(f"✅")
|
||||
else:
|
||||
print(f"❌")
|
||||
print(f"{error}")
|
||||
raise Exception(f"{str(error)}")
|
||||
return result
|
||||
except Exception as e:
|
||||
print(f"SSH error: {str(e)} --- {str(error)}", flush=True)
|
||||
raise
|
||||
finally:
|
||||
client.close()
|
||||
|
||||
def execute_sudo_ssh_command(ip, command, client):
|
||||
try:
|
||||
quoted_command = f"bash -c {shlex.quote(command)}"
|
||||
sudo_command = f"sudo -S -p '' {quoted_command}"
|
||||
stdin, stdout, stderr = client.exec_command(sudo_command, timeout=180)
|
||||
time.sleep(3)
|
||||
stdin.write(ssh_password + '\n')
|
||||
stdin.flush()
|
||||
exit_status = stdout.channel.recv_exit_status()
|
||||
|
||||
output = stdout.read().decode('utf-8')
|
||||
error = stderr.read().decode('utf-8')
|
||||
if exit_status == 0:
|
||||
|
||||
print(f"{output}")
|
||||
else:
|
||||
print(f"❌")
|
||||
print(f"{error}")
|
||||
raise Exception("Error during SSH sudo command.")
|
||||
|
||||
result = stdout.read().decode().lower().strip()
|
||||
return result
|
||||
except Exception as e:
|
||||
raise
|
||||
finally:
|
||||
client.close()
|
||||
|
||||
def scp_get_file(ip, remote_path, local_path):
|
||||
client = SSHClient()
|
||||
client.set_missing_host_key_policy(AutoAddPolicy())
|
||||
|
||||
local_path = os.path.expanduser(local_path)
|
||||
local_path = os.path.abspath(local_path)
|
||||
|
||||
local_dir = os.path.dirname(local_path)
|
||||
if local_dir:
|
||||
os.makedirs(local_dir, exist_ok=True)
|
||||
|
||||
|
||||
try:
|
||||
client.connect(
|
||||
ip,
|
||||
port=11022,
|
||||
username=ssh_username,
|
||||
password=ssh_password,
|
||||
allow_agent=False,
|
||||
look_for_keys=False,
|
||||
timeout=180
|
||||
)
|
||||
|
||||
client.get_transport().set_keepalive(5)
|
||||
with SCPClient(client.get_transport()) as scp:
|
||||
scp.get(remote_path, local_path)
|
||||
|
||||
except Exception as e:
|
||||
raise
|
||||
finally:
|
||||
client.close()
|
||||
|
||||
def main():
|
||||
|
||||
print(f"Starting...\n", flush=True)
|
||||
|
||||
for i in ip_address_range:
|
||||
ip_address = f"{ip_address_prefix}{i}"
|
||||
print(f"[{time.ctime(time.time())}] {str(i)} ({ip_address})", end=" ", flush=True)
|
||||
|
||||
try:
|
||||
activate_ssh(ip_address)
|
||||
except Exception as e:
|
||||
print(f"SSH activation failed for {ip_address}:", flush=True)
|
||||
print(f"{e}", flush=True)
|
||||
print(f"Skipping CUBE...", flush=True)
|
||||
continue
|
||||
|
||||
cube_id = "NA"
|
||||
|
||||
client = paramiko.SSHClient()
|
||||
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
client.connect(ip_address, port=11022, username=ssh_username, password=ssh_password, allow_agent=False, look_for_keys=False, timeout=180, banner_timeout=180)
|
||||
client.get_transport().set_keepalive(5)
|
||||
|
||||
try:
|
||||
cube_id = execute_ssh_command(ip_address, "hostname", client)
|
||||
print(f"{cube_id} ✅", flush=True)
|
||||
except Exception as e:
|
||||
print(f"cube-xxxxx ❌", flush=True)
|
||||
print(f"Error getting hostname for {ip_address}:", flush=True)
|
||||
print(f"{e}", flush=True)
|
||||
print(f"Skipping CUBE...", flush=True)
|
||||
continue
|
||||
|
||||
try:
|
||||
result = execute_ssh_command(ip_address, "cat /etc/cube", client)
|
||||
print(f"{result}", flush=True)
|
||||
except Exception as e:
|
||||
print(f"Error getting Cloud settings for {cube_id}.", flush=True)
|
||||
print(f"{e}", flush=True)
|
||||
print(f"Skipping CUBE...", flush=True)
|
||||
continue
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user