53 lines
1.6 KiB
Bash
53 lines
1.6 KiB
Bash
|
|
#!/usr/bin/env bash
|
||
|
|
# Shared SSH Helper Module
|
||
|
|
# Provides common SSH utility functions
|
||
|
|
# Usage: source "$(dirname "${BASH_SOURCE[0]}")/../lib/ssh-helpers.sh"
|
||
|
|
|
||
|
|
# Source dependencies
|
||
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||
|
|
source "$SCRIPT_DIR/ip-config.sh" 2>/dev/null || true
|
||
|
|
source "$SCRIPT_DIR/logging.sh" 2>/dev/null || true
|
||
|
|
|
||
|
|
# SSH user: when using SSH + pct, root is not needed (use a user with pct permissions)
|
||
|
|
PROXMOX_SSH_USER="${PROXMOX_SSH_USER:-$USER}"
|
||
|
|
|
||
|
|
# SSH helper functions
|
||
|
|
ssh_proxmox() {
|
||
|
|
local host="${1:-${PROXMOX_HOST_ML110:-192.168.11.10}}"
|
||
|
|
local command="${2:-}"
|
||
|
|
|
||
|
|
if [ -z "$command" ]; then
|
||
|
|
log_error "SSH command required"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no "${PROXMOX_SSH_USER}@$host" "$command"
|
||
|
|
}
|
||
|
|
|
||
|
|
ssh_container() {
|
||
|
|
local host="${1:-${PROXMOX_HOST_ML110:-192.168.11.10}}"
|
||
|
|
local vmid="${2:-}"
|
||
|
|
local command="${3:-}"
|
||
|
|
|
||
|
|
if [ -z "$vmid" ] || [ -z "$command" ]; then
|
||
|
|
log_error "VMID and command required"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no "${PROXMOX_SSH_USER}@$host" \
|
||
|
|
"pct exec $vmid -- $command"
|
||
|
|
}
|
||
|
|
|
||
|
|
# Test SSH connectivity
|
||
|
|
test_ssh_connection() {
|
||
|
|
local host="${1:-${PROXMOX_HOST_ML110:-192.168.11.10}}"
|
||
|
|
|
||
|
|
if ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no "${PROXMOX_SSH_USER}@$host" "echo 'SSH OK'" >/dev/null 2>&1; then
|
||
|
|
log_success "SSH connection to $host: OK"
|
||
|
|
return 0
|
||
|
|
else
|
||
|
|
log_error "SSH connection to $host: FAILED"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
}
|