# 放到$PROFILE # SSH config 路径(自动适配你的用户名,推荐) $sshConfigPath = Join-Path $env:USERPROFILE ".ssh\config" # ==================== 内部帮助函数(无需手动调用) ==================== function Get-SshConfigEntries { $entries = @() $current = $null if (-not (Test-Path $sshConfigPath)) { return $entries } Get-Content $sshConfigPath | ForEach-Object { $line = ($_ -replace '#.*$', '').Trim() if ([string]::IsNullOrWhiteSpace($line)) { return } if ($line -match '^Host\s+(.+)$') { if ($current) { $entries += $current } $current = [PSCustomObject]@{ Host = $matches[1].Trim() HostName = $null Port = $null User = $null } } elseif ($current) { if ($line -match '^HostName\s+(.+)$') { $current.HostName = $matches[1].Trim() } elseif ($line -match '^Port\s+(.+)$') { $current.Port = $matches[1].Trim() } elseif ($line -match '^User\s+(.+)$') { $current.User = $matches[1].Trim() } } } if ($current) { $entries += $current } return $entries } function Save-SshConfigEntries { param($entries) $content = @() foreach ($entry in $entries) { $content += "Host $($entry.Host)" if ($entry.HostName) { $content += " HostName $($entry.HostName)" } if ($entry.Port) { $content += " Port $($entry.Port)" } if ($entry.User) { $content += " User $($entry.User)" } $content += "" # 每个 Host 之间空一行 } # 自动创建 .ssh 目录 + 备份原文件 $dir = Split-Path $sshConfigPath -Parent if (-not (Test-Path $dir)) { New-Item -Path $dir -ItemType Directory -Force | Out-Null } if (Test-Path $sshConfigPath) { Copy-Item $sshConfigPath "$sshConfigPath.bak" -Force Write-Host "已备份原 config → $sshConfigPath.bak" -ForegroundColor Yellow } $content | Set-Content $sshConfigPath -Encoding UTF8 Write-Host "✅ SSH config 已保存!" -ForegroundColor Green } # ==================== 1. 查看所有 Host(原来 sshhosts) ==================== function Get-SshHost { $entries = Get-SshConfigEntries if ($entries.Count -eq 0) { Write-Host "未在 config 中找到任何 Host 配置。" -ForegroundColor Yellow return } Write-Host "✅ SSH Config 中的所有配置:" -ForegroundColor Green $entries | Sort-Object Host | Format-Table -AutoSize ` @{Name='名字 (Host)'; Expression={$_.Host}}, ` @{Name='主机 (HostName)'; Expression={$_.HostName}}, ` @{Name='端口 (Port)'; Expression={$_.Port}}, ` @{Name='用户 (User)'; Expression={$_.User}} } # ==================== 2. 添加/更新 Host ==================== function Add-SshHost { param( [Parameter(Mandatory=$true, Position=0)] [string]$Name, # ← 你取的“名字”(Host) [Parameter(Mandatory=$true, Position=1)] [string]$HostName, # ← 实际的“主机/IP”(HostName) [Parameter(Mandatory=$false)] [string]$Port, # ← 端口(可选) [Parameter(Mandatory=$false)] [string]$User # ← 用户名(可选) ) $entries = Get-SshConfigEntries # 如果已存在就更新,否则新增 $existing = $entries | Where-Object { $_.Host -eq $Name } if ($existing) { Write-Host "⚠️ Host '$Name' 已存在,正在更新..." -ForegroundColor Yellow $existing.HostName = $HostName if ($PSBoundParameters.ContainsKey('Port')) { $existing.Port = $Port } if ($PSBoundParameters.ContainsKey('User')) { $existing.User = $User } } else { $newEntry = [PSCustomObject]@{ Host = $Name.Trim() HostName = $HostName.Trim() Port = if ($PSBoundParameters.ContainsKey('Port')) { $Port } else { $null } User = if ($PSBoundParameters.ContainsKey('User')) { $User } else { $null } } $entries += $newEntry Write-Host "✅ 新增 Host '$Name'" -ForegroundColor Green } Save-SshConfigEntries $entries } # ==================== 3. 移除 Host ==================== function Remove-SshHost { param( [Parameter(Mandatory=$true)] [string]$Name ) $entries = Get-SshConfigEntries $toRemove = $entries | Where-Object { $_.Host -eq $Name } if (-not $toRemove) { Write-Host "❌ 未找到 Host '$Name',无需移除。" -ForegroundColor Red return } $entries = $entries | Where-Object { $_.Host -ne $Name } Save-SshConfigEntries $entries Write-Host "✅ 已成功移除 Host '$Name'" -ForegroundColor Green } # ==================== 创建快捷 alias ==================== New-Alias -Name sg -Value Get-SshHost -Force -Scope Global New-Alias -Name sa -Value Add-SshHost -Force -Scope Global New-Alias -Name sr -Value Remove-SshHost -Force -Scope Global Write-Host "🎉 Custom commands loaded successfully" -ForegroundColor Cyan Write-Host "Available:" -NoNewline Write-Host " sg | sa | sr" -ForegroundColor White