-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh.ps1
More file actions
168 lines (143 loc) · 5.41 KB
/
ssh.ps1
File metadata and controls
168 lines (143 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# ssh.ps1 — SSH Key Launcher (with alias support and menu)
# https://github.com/markcross/windows-ssh
# wt.exe powershell -NoExit -File "C:\Users\mark.cross\ssh.ps1"
# doesn't work
$ConfigFile = "$env:USERPROFILE\ssh-config-menu"
# --- Detect if we should add keys ---
$ShouldAddKeys = $false
if ($args.Length -gt 0 -and $args[0].ToLower() -eq 'add') {
$ShouldAddKeys = $true
}
if (-not (Test-Path $ConfigFile)) {
Write-Host "❌ Config file not found: $ConfigFile"
exit 1
}
# --- Read and parse config ---
$sshDir = ""
$privateKeys = @()
$hosts = @()
foreach ($lineRaw in Get-Content $ConfigFile) {
$line = $lineRaw.Trim()
if ($line -match '^\s*\.ssh\s*=\s*"(.*?)"') {
$sshDir = $Matches[1] -replace '\$env:USERPROFILE', $env:USERPROFILE
$sshDir = $sshDir.Trim('"')
}
elseif ($line -ne "" -and $line -notmatch '^#' -and ($line -match '\.pem' -or $line -match '^id_')) {
$privateKeys += $line
}
elseif ($line -ne "" -and $line -notmatch '^#' -and $line -match '^[^#]+#[^#]+#[^#]+#[^#]+$') {
$hosts += $line
}
}
if (-not $sshDir) { $sshDir = "$env:USERPROFILE\.ssh" }
$sshDir = $sshDir.TrimEnd('\')
# --- Load all SSH keys ---
function Get-LoadedKeys {
try {
return (& ssh-add -L 2>$null)
} catch {
return @()
}
}
$LoadedKeys = Get-LoadedKeys
function Ensure-All-Keys-Added {
param($keyList, $sshDir)
foreach ($keyName in $keyList) {
$keyPath = "$sshDir\$keyName"
if (-not (Test-Path $keyPath)) {
Write-Host "⚠️ Key not found: $keyPath" -ForegroundColor Yellow
continue
}
$isLoaded = $false
foreach ($line in $LoadedKeys) {
if ($line -like "*$keyName*") { $isLoaded = $true; break }
}
if (-not $isLoaded) {
Write-Host "`n🔐 Adding SSH key: $keyPath"
& ssh-add "$keyPath"
$LoadedKeys = Get-LoadedKeys
}
}
}
# --- Only add keys if requested ---
if ($ShouldAddKeys) {
Ensure-All-Keys-Added -keyList $privateKeys -sshDir $sshDir
}
Write-Host "`n✅ Currently loaded SSH keys (via ssh-agent):" -ForegroundColor Green
try {
$agentKeys = & ssh-add -L
if ($agentKeys.Count -eq 0) {
Write-Host "❌ No keys currently loaded in ssh-agent." -ForegroundColor Red
} else {
$agentKeys | ForEach-Object { Write-Host " • $_" }
}
} catch {
Write-Host "⚠️ Unable to retrieve keys from ssh-agent." -ForegroundColor Yellow
}
Start-Sleep -Seconds 3
# --- Build host menu items ---
function Build-MenuItems {
param($hosts, $sshDir)
$items = @()
foreach ($line in $hosts) {
$parts = $line -split '#'
if ($parts.Count -eq 4) {
$items += @{
Alias = $parts[0]
Host = $parts[1]
Username = $parts[2]
Keyname = $parts[3]
KeyPath = "$sshDir\$($parts[3])"
}
}
}
return $items
}
# --- Main menu loop ---
do {
Clear-Host
$menuItems = Build-MenuItems $hosts $sshDir
$exitIndex = $menuItems.Count
$configIndex = $exitIndex + 1 # New menu index for config editor
Write-Host "`n📡 SSH Quick Connect Menu" -ForegroundColor Cyan
for ($i = 0; $i -lt $menuItems.Count; $i++) {
$entry = $menuItems[$i]
Write-Host ("[{0}] {1}@{2} ({3}) [key: {4}]" -f $i, $entry.Username, $entry.Host, $entry.Alias, $entry.Keyname)
}
Write-Host ("[{0}] Exit" -f $exitIndex)
Write-Host ("[{0}] Configure SSH Menu (edit config file)" -f $configIndex)
do {
$choiceRaw = Read-Host "`nEnter the number of your choice"
$isValid = $choiceRaw -match '^\d+$' -and [int]$choiceRaw -ge 0 -and [int]$choiceRaw -le $configIndex
if (-not $isValid) {
Write-Host "❌ Invalid choice. Please enter a number between 0 and $configIndex."
}
} while (-not $isValid)
$choice = [int]$choiceRaw
if ($choice -eq $exitIndex) {
Write-Host "`n👋 Exiting."
break
}
elseif ($choice -eq $configIndex) {
Write-Host "`n📝 Opening config file in Notepad: $ConfigFile"
Start-Process notepad.exe $ConfigFile
break
}
$selected = $menuItems[$choice]
$keyPath = $selected.KeyPath
$user = $selected.Username
$targetHost = $selected.Host
$sshCmd = "ssh -i `"$keyPath`" $user@$targetHost"
Write-Host "`n🚀 Connecting to $user@$targetHost"
Write-Host "Running: $sshCmd"
Start-Process powershell.exe -ArgumentList "-NoExit", "-Command", $sshCmd
# Start-Process wt.exe -ArgumentList "new-tab", "powershell", "-NoExit", "-Command", $sshCmd # Didn't work to spawn to a tab instead of new window
Write-Host "`nPress Enter to return to menu..."
[void][System.Console]::ReadLine()
} while ($true)
# I'm currently using Windows Terminal to front WSL 2 Ubuntu. I use it to SSH out to multiple servers.
# I'm now at a point where I'd need to run similar \ identical tasks across multiple servers,
# so I'd like to be able to say Open a new pane and SSH to server Foo, Bar and FooBar.
# I've got the server config stored in .ssh/config and SSH key access sorted.
#
# https://stackoverflow.com/questions/67381475/windows-terminal-script-opening-panes-and-sshing-to-servers