myservers/myservers.py

60 lines
1.5 KiB
Python
Raw Normal View History

2025-08-27 12:34:37 -04:00
# /// script
# dependencies = [
# "paramiko",
# ]
# ///
import paramiko
# Server configurations
servers = [
{
"host": "server3.example.com",
"user": "root",
"password": "your_password"
},
{
"host": "192.168.1.100",
"user": "deploy",
"key": "~/.ssh/deploy_key",
"port": 2222 # Optional: custom SSH port
}
]
for server in servers:
print(f"\n--- {server['host']} ---")
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
# Get connection parameters
host = server["host"]
user = server["user"]
port = server.get("port", 22)
# Connect based on available auth method
if "password" in server:
ssh.connect(host, port=port, username=user, password=server["password"])
elif "key" in server:
ssh.connect(host, port=port, username=user, key_filename=server["key"])
else:
# Try default key locations
ssh.connect(host, port=port, username=user)
# Execute command
stdin, stdout, stderr = ssh.exec_command("docker ps")
output = stdout.read().decode()
error = stderr.read().decode()
if output:
print(output)
if error:
print(f"Error: {error}")
except Exception as e:
print(f"Failed: {e}")
finally:
ssh.close()