Auto-detect server IP from SSH known_hosts
This commit is contained in:
1 parent
0ac3409a23
commit
ebff79dbeb
3 files changed
+33
-1
No files matched your search
@@ -161,6 +161,19 @@ $FileBrowserPort = "18888"
|
|||||||
$AuthUser = "admin"
|
$AuthUser = "admin"
|
||||||
$AuthPass = "changeme"
|
$AuthPass = "changeme"
|
||||||
|
|
||||||
|
# Try to detect server IP from known_hosts
|
||||||
|
$KnownHosts = Join-Path $env:USERPROFILE ".ssh\known_hosts"
|
||||||
|
if (Test-Path $KnownHosts) {
|
||||||
|
$hosts = Select-String -Path $KnownHosts -Pattern '^\d+\.\d+\.\d+\.\d+' | ForEach-Object {
|
||||||
|
if ($_ -match '^(\d+\.\d+\.\d+\.\d+)') { $matches[1] }
|
||||||
|
} | Select-Object -Unique
|
||||||
|
if ($hosts.Count -gt 0) {
|
||||||
|
$ServerHost = $hosts[-1]
|
||||||
|
Write-Host " Detected previous server IP: $ServerHost" -ForegroundColor Green
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Test SSH if we have a key and a real IP
|
||||||
if ($FoundKey -and $ServerHost -ne "YOUR_SERVER_IP") {
|
if ($FoundKey -and $ServerHost -ne "YOUR_SERVER_IP") {
|
||||||
# Test SSH
|
# Test SSH
|
||||||
$testOut = ssh -i $FoundKey -o StrictHostKeyChecking=accept-new -o ConnectTimeout=10 -o BatchMode=yes "dune@$ServerHost" "echo ok" 2>$null
|
$testOut = ssh -i $FoundKey -o StrictHostKeyChecking=accept-new -o ConnectTimeout=10 -o BatchMode=yes "dune@$ServerHost" "echo ok" 2>$null
|
||||||
|
|||||||
@@ -130,6 +130,17 @@ FILEBROWSER_PORT="18888"
|
|||||||
AUTH_USER="admin"
|
AUTH_USER="admin"
|
||||||
AUTH_PASS="changeme"
|
AUTH_PASS="changeme"
|
||||||
|
|
||||||
|
# Try to detect server IP from known_hosts
|
||||||
|
KNOWN_HOSTS="$HOME/.ssh/known_hosts"
|
||||||
|
if [ -f "$KNOWN_HOSTS" ]; then
|
||||||
|
DETECTED_IP=$(grep -oE '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' "$KNOWN_HOSTS" | tail -1)
|
||||||
|
if [ -n "$DETECTED_IP" ]; then
|
||||||
|
SERVER_HOST="$DETECTED_IP"
|
||||||
|
echo " Detected previous server IP: $SERVER_HOST"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Test SSH if we have a key and a real IP
|
||||||
if [ -n "$FOUND_KEY" ] && [ "$SERVER_HOST" != "YOUR_SERVER_IP" ]; then
|
if [ -n "$FOUND_KEY" ] && [ "$SERVER_HOST" != "YOUR_SERVER_IP" ]; then
|
||||||
if ssh -i "$FOUND_KEY" -o StrictHostKeyChecking=accept-new -o ConnectTimeout=10 -o BatchMode=yes "dune@$SERVER_HOST" "echo ok" &>/dev/null; then
|
if ssh -i "$FOUND_KEY" -o StrictHostKeyChecking=accept-new -o ConnectTimeout=10 -o BatchMode=yes "dune@$SERVER_HOST" "echo ok" &>/dev/null; then
|
||||||
echo " SSH connection OK"
|
echo " SSH connection OK"
|
||||||
|
|||||||
@@ -133,9 +133,17 @@ Write-Host "[OK] SSH tunnel up on localhost:$LocalPort" -ForegroundColor Green
|
|||||||
|
|
||||||
# Start DB port-forward on VM
|
# Start DB port-forward on VM
|
||||||
Write-Host "[2/4] Starting DB port-forward on VM..." -ForegroundColor Yellow
|
Write-Host "[2/4] Starting DB port-forward on VM..." -ForegroundColor Yellow
|
||||||
|
|
||||||
|
if (-not $Namespace -or $Namespace -eq '') {
|
||||||
|
Write-Host "[ERROR] Kubernetes namespace is empty." -ForegroundColor Red
|
||||||
|
Write-Host " Edit settings.yaml and set kubernetes.namespace, or re-run setup.ps1 with SSH access." -ForegroundColor Yellow
|
||||||
|
Stop-Process -Id $sshTunnel.Id -Force -ErrorAction SilentlyContinue
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
$DBService = "${Namespace}-db-dbdepl-svc"
|
$DBService = "${Namespace}-db-dbdepl-svc"
|
||||||
|
|
||||||
# Try to find the actual DB service name if the default fails
|
# Try to find the actual DB service name
|
||||||
$pfCheck = ssh -i $SSHKey -o StrictHostKeyChecking=accept-new -o ConnectTimeout=10 ${SSHUser}@${SSHHost} "sudo kubectl get svc -n ${Namespace} -o name" 2>$null
|
$pfCheck = ssh -i $SSHKey -o StrictHostKeyChecking=accept-new -o ConnectTimeout=10 ${SSHUser}@${SSHHost} "sudo kubectl get svc -n ${Namespace} -o name" 2>$null
|
||||||
if ($pfCheck) {
|
if ($pfCheck) {
|
||||||
$dbSvc = ($pfCheck -split "`n") | Where-Object { $_ -match 'db.*svc' -or $_ -match 'postgres' -or $_ -match 'pg' } | Select-Object -First 1
|
$dbSvc = ($pfCheck -split "`n") | Where-Object { $_ -match 'db.*svc' -or $_ -match 'postgres' -or $_ -match 'pg' } | Select-Object -First 1
|
||||||
|
|||||||
Reference in new issue
Block a user