Files
claw/claw.ps1
T

204 lines
7.4 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
"@
}
# ============================================================
# Load host config
# ============================================================
function Load-Host($Name) {
$HostFile = Join-Path $ClawRoot "hosts\$Name"
if (-not (Test-Path $HostFile)) {
Write-Host "ERROR: host '$Name' not found (hosts\$Name)" -ForegroundColor Red
Write-Host "Available hosts:" -ForegroundColor Yellow
List-Hosts
exit 1
}
$script:HostIP = $null
$script:HostUser = "root"
$script:HostPort = "22"
$script:HostKey = $null
Get-Content $HostFile | ForEach-Object {
$line = $_.Trim()
if ($line -match '^\s*#' -or $line -eq '') { return }
if ($line -match '^HOST=(.+)$') { $script:HostIP = $matches[1].Trim() }
if ($line -match '^USER=(.+)$') { $script:HostUser = $matches[1].Trim() }
if ($line -match '^PORT=(.+)$') { $script:HostPort = $matches[1].Trim() }
if ($line -match '^KEY=(.+)$') { $script:HostKey = $matches[1].Trim() }
}
if (-not $HostIP) {
Write-Host "ERROR: hosts\$Name missing HOST=" -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 file in hosts\)"
return
}
Get-ChildItem $hostDir | ForEach-Object {
$name = $_.Name
$h = ""; $u = "root"; $p = "22"
Get-Content $_.FullName | ForEach-Object {
$line = $_.Trim()
if ($line -match '^HOST=(.+)$') { $h = $matches[1].Trim() }
if ($line -match '^USER=(.+)$') { $u = $matches[1].Trim() }
if ($line -match '^PORT=(.+)$') { $p = $matches[1].Trim() }
}
Write-Host (" {0,-20} {1}@{2}:{3}" -f $name, $u, $h, $p)
}
}
# ============================================================
# 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, use: 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 }
}