Health

 # === CONFIG ===

$puttyPath  = "C:\Program Files\PuTTY\putty.exe"  # Path to PuTTY

$jumpUser   = "vk"                                # Your username on jumpserver

$jumpServer = "ipagbld"                           # Jumpserver hostname


# === INPUT: Group + optional SubGroup ===

$groupName = Read-Host "Enter group name (e.g. web, db, app)"

$subGroupName = Read-Host "Enter sub group name (or press Enter for all in group)"


# === Load matching hosts ===

$hosts = Import-Csv "hosts.csv" | Where-Object { $_.Group -eq $groupName }


if ($subGroupName) {

    $hosts = $hosts | Where-Object { $_.SubGroup -eq $subGroupName }

}


if (-not $hosts) {

    Write-Host "No hosts found for selection." -ForegroundColor Red

    exit

}


# === Preview mode ===

Write-Host "About to connect to:" -ForegroundColor Cyan

foreach ($h in $hosts) {

    Write-Host "  $($h.Host) ($($h.User)) → $($h.CommandFile)"

}

$confirm = Read-Host "Proceed? (Y/N)"

if ($confirm -ne 'Y') { exit }


# === Loop through hosts ===

foreach ($h in $hosts) {

    $cmdFile = $h.CommandFile

    if (-not (Test-Path $cmdFile)) {

        Write-Host "Command file not found: $cmdFile" -ForegroundColor Red

        continue

    }

    $commands = Get-Content $cmdFile -Raw

    $remoteCommand = "ssh -t $($h.User)@$($h.Host) '$commands'"

    Start-Process $puttyPath -ArgumentList "$jumpUser@$jumpServer -t $remoteCommand"

}








Group,SubGroup,Host,User,CommandFile

web,ol,web1.example.com,ec2-user,commands/web1.txt

web,al,web2.example.com,ubuntu,commands/web2.txt

web,ml,web3.example.com,ec2-user,commands/web3.txt

db,ol,db1.example.com,admin,commands/db1.txt

db,al,db2.example.com,oracle,commands/db2.txt

app,ml,app1.example.com,ec2-user,commands/app1.txt

Comments