242 lines
8.6 KiB
PowerShell
242 lines
8.6 KiB
PowerShell
# Claw - Cloud Host Manager (PowerShell)
|
|
param($Command, $Arg1, $Arg2, $Arg3)
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
$ClawRoot = $PSScriptRoot
|
|
|
|
function Usage {
|
|
@"
|
|
Claw - Cloud Host Manager
|
|
|
|
Usage: .\claw.ps1 <command> [args...]
|
|
claw <command> [args...]
|
|
|
|
Commands:
|
|
list List all hosts
|
|
ssh <host> SSH into a host
|
|
run <host> <script> Run a script on a host (from scripts\)
|
|
push <host> <file/dir> Push file/dir to host (from payloads\)
|
|
key add <name> <pem> Register a PEM key into keys\
|
|
key list List all registered keys
|
|
|
|
Examples:
|
|
claw list
|
|
claw ssh my-server
|
|
claw run my-server hello.sh
|
|
claw push my-server my-app
|
|
claw key add my-server C:\Users\me\Downloads\key.pem
|
|
"@
|
|
}
|
|
|
|
# ============================================================
|
|
# Minimal YAML parser (handles nested key: value, no arrays)
|
|
# ============================================================
|
|
function Parse-Yaml($Path) {
|
|
$result = @{}
|
|
$stack = New-Object System.Collections.ArrayList
|
|
$null = $stack.Add(@{ node = $result; indent = -1; key = $null })
|
|
foreach ($line in (Get-Content $Path -Encoding UTF8)) {
|
|
$trimmed = $line.TrimEnd()
|
|
if ($trimmed -match '^\s*#' -or $trimmed -match '^\s*$') { continue }
|
|
if ($trimmed -notmatch '^(\s*)([^:]+):\s*(.*)') { continue }
|
|
$indent = $matches[1].Length
|
|
$key = $matches[2].Trim()
|
|
$value = $matches[3].Trim()
|
|
|
|
# Pop stack until we find the parent with smaller indent
|
|
while ($stack.Count -gt 1 -and $stack[-1].indent -ge $indent) {
|
|
$null = $stack.RemoveAt($stack.Count - 1)
|
|
}
|
|
$parent = $stack[-1].node
|
|
|
|
if ($value) {
|
|
$parent[$key] = $value
|
|
} else {
|
|
$child = @{}
|
|
$parent[$key] = $child
|
|
$null = $stack.Add(@{ node = $child; indent = $indent; key = $key })
|
|
}
|
|
}
|
|
return $result
|
|
}
|
|
|
|
# ============================================================
|
|
# Load host config
|
|
# ============================================================
|
|
function Load-Host($Name) {
|
|
$HostFile = Join-Path $ClawRoot "hosts\$Name.yml"
|
|
if (-not (Test-Path $HostFile)) {
|
|
# fallback: try without .yml extension (old format)
|
|
$HostFile = Join-Path $ClawRoot "hosts\$Name"
|
|
if (-not (Test-Path $HostFile)) {
|
|
Write-Host "ERROR: host '$Name' not found (hosts\$Name.yml)" -ForegroundColor Red
|
|
Write-Host "Available hosts:" -ForegroundColor Yellow
|
|
List-Hosts
|
|
exit 1
|
|
}
|
|
}
|
|
|
|
$cfg = Parse-Yaml $HostFile
|
|
$h = $cfg.host
|
|
if (-not $h) {
|
|
Write-Host "ERROR: hosts\$Name missing 'host:' section" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
|
|
$script:HostIP = if ($h.ip) { $h.ip } else { $null }
|
|
$script:HostUser = if ($h.user) { $h.user } else { "root" }
|
|
$script:HostPort = if ($h.port) { $h.port } else { "22" }
|
|
$script:HostKey = if ($h.key) { $h.key } else { $null }
|
|
|
|
if (-not $HostIP) {
|
|
Write-Host "ERROR: hosts\${Name}: host.ip is required" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
|
|
if ($HostKey) {
|
|
$script:HostKey = Join-Path $ClawRoot $HostKey
|
|
if (-not (Test-Path $HostKey)) {
|
|
Write-Host "ERROR: key file not found: $HostKey" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
# Fix Windows permissions (SSH requires key NOT readable by other users)
|
|
icacls $HostKey /inheritance:r /grant "${env:USERNAME}:(R)" 2>$null | Out-Null
|
|
}
|
|
}
|
|
|
|
# ============================================================
|
|
# Build SSH args
|
|
# ============================================================
|
|
function Build-SshArgs {
|
|
$a = @("-o", "StrictHostKeyChecking=accept-new")
|
|
if ($HostPort) { $a += "-p"; $a += $HostPort }
|
|
if ($HostKey) { $a += "-i"; $a += $HostKey }
|
|
return $a
|
|
}
|
|
|
|
# ============================================================
|
|
# List all hosts
|
|
# ============================================================
|
|
function List-Hosts {
|
|
$hostDir = Join-Path $ClawRoot "hosts"
|
|
if (-not (Test-Path $hostDir) -or @(Get-ChildItem $hostDir).Count -eq 0) {
|
|
Write-Host " (no hosts yet, create a .yml file in hosts\)"
|
|
return
|
|
}
|
|
Get-ChildItem $hostDir -Filter *.yml | ForEach-Object {
|
|
$name = $_.BaseName
|
|
try {
|
|
$cfg = Parse-Yaml $_.FullName
|
|
$h = $cfg.host
|
|
if ($h) {
|
|
$ip = if ($h.ip) { $h.ip } else { "?" }
|
|
$u = if ($h.user) { $h.user } else { "root" }
|
|
$p = if ($h.port) { $h.port } else { "22" }
|
|
Write-Host (" {0,-25} {1}@{2}:{3}" -f $name, $u, $ip, $p)
|
|
}
|
|
} catch {
|
|
Write-Host (" {0,-25} (parse error)" -f $name)
|
|
}
|
|
}
|
|
}
|
|
|
|
# ============================================================
|
|
# SSH connect
|
|
# ============================================================
|
|
function Invoke-Ssh($Name) {
|
|
Load-Host $Name
|
|
$sa = Build-SshArgs
|
|
Write-Host ">>> Connecting to $Name ($HostUser@$HostIP`:$HostPort)" -ForegroundColor Green
|
|
& ssh @sa "$HostUser@$HostIP"
|
|
}
|
|
|
|
# ============================================================
|
|
# Run script on host
|
|
# ============================================================
|
|
function Invoke-Run($Name, $Script) {
|
|
Load-Host $Name
|
|
$sp = Join-Path $ClawRoot "scripts\$Script"
|
|
if (-not (Test-Path $sp)) {
|
|
Write-Host "ERROR: script '$Script' not found (scripts\$Script)" -ForegroundColor Red
|
|
Write-Host "Available scripts:" -ForegroundColor Yellow
|
|
Get-ChildItem (Join-Path $ClawRoot "scripts") | ForEach-Object { Write-Host " $($_.Name)" }
|
|
exit 1
|
|
}
|
|
$sa = Build-SshArgs
|
|
Write-Host ">>> Running $Script on $Name" -ForegroundColor Green
|
|
Get-Content $sp -Raw | & ssh @sa "$HostUser@$HostIP" "bash -s"
|
|
}
|
|
|
|
# ============================================================
|
|
# Push files to host
|
|
# ============================================================
|
|
function Invoke-Push($Name, $Target) {
|
|
Load-Host $Name
|
|
$src = Join-Path $ClawRoot "payloads\$Target"
|
|
if (-not (Test-Path $src)) {
|
|
Write-Host "ERROR: '$Target' not found (payloads\$Target)" -ForegroundColor Red
|
|
Write-Host "Available payloads:" -ForegroundColor Yellow
|
|
Get-ChildItem (Join-Path $ClawRoot "payloads") | ForEach-Object { Write-Host " $($_.Name)" }
|
|
exit 1
|
|
}
|
|
|
|
$sca = @("-o", "StrictHostKeyChecking=accept-new")
|
|
if ($HostPort) { $sca += "-P"; $sca += $HostPort }
|
|
if ($HostKey) { $sca += "-i"; $sca += $HostKey }
|
|
if (Test-Path $src -PathType Container) { $sca += "-r" }
|
|
|
|
Write-Host ">>> Pushing $Target -> $Name`:$HostUser@$HostIP`:/tmp/" -ForegroundColor Green
|
|
& scp @sca $src "$HostUser@$HostIP`:/tmp/"
|
|
Write-Host ">>> Done. Files at /tmp/$Target" -ForegroundColor Green
|
|
}
|
|
|
|
# ============================================================
|
|
# Key management
|
|
# ============================================================
|
|
function Invoke-Key($Action, $Name, $Src) {
|
|
switch ($Action) {
|
|
"add" {
|
|
if (-not $Name -or -not $Src) {
|
|
Write-Host "Usage: claw key add <name> <path-to.pem>" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
if (-not (Test-Path $Src)) {
|
|
Write-Host "ERROR: file not found: $Src" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
$dest = Join-Path $ClawRoot "keys\$Name.pem"
|
|
Copy-Item $Src $dest -Force
|
|
Write-Host ">>> Key saved: keys\$Name.pem" -ForegroundColor Green
|
|
Write-Host ">>> In host file, set: host.key: keys\$Name.pem"
|
|
}
|
|
"list" {
|
|
Write-Host "Registered keys:" -ForegroundColor Yellow
|
|
$kd = Join-Path $ClawRoot "keys"
|
|
if ((Test-Path $kd) -and @(Get-ChildItem $kd -Filter *.pem).Count -gt 0) {
|
|
Get-ChildItem $kd -Filter *.pem | ForEach-Object {
|
|
Write-Host " $($_.Name)"
|
|
}
|
|
} else {
|
|
Write-Host " (none)"
|
|
}
|
|
}
|
|
default {
|
|
Write-Host "Usage: claw key <add|list>" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
}
|
|
}
|
|
|
|
# ============================================================
|
|
# Main
|
|
# ============================================================
|
|
switch ($Command) {
|
|
"list" { Write-Host "Hosts:" -ForegroundColor Yellow; List-Hosts }
|
|
"ssh" { Invoke-Ssh $Arg1 }
|
|
"run" { Invoke-Run $Arg1 $Arg2 }
|
|
"push" { Invoke-Push $Arg1 $Arg2 }
|
|
"key" { Invoke-Key $Arg1 $Arg2 $Arg3 }
|
|
{ $_ -in @("-h", "--help", "help", "", $null) } { Usage }
|
|
default { Write-Host "Unknown command: $Command" -ForegroundColor Red; Usage; exit 1 }
|
|
}
|