@@ -24,9 +24,13 @@ if (-not $config) {
2424
2525# Get servers from config
2626$servers = @ {}
27+ $serverKeyFiles = @ {}
2728if ($config.ssh.servers ) {
2829 foreach ($key in $config.ssh.servers.PSObject.Properties.Name ) {
2930 $servers [$key ] = $config.ssh.servers .$key.hostname
31+ if ($config.ssh.servers .$key.keyFile ) {
32+ $serverKeyFiles [$key ] = $config.ssh.servers .$key.keyFile
33+ }
3034 }
3135}
3236
@@ -60,39 +64,90 @@ if ($LocalPort -eq 0) {
6064 $LocalPort = $RemotePort
6165}
6266
63- # Resolve hostname
67+ # Resolve hostname and key file
68+ $keyFile = $null
6469if ($servers.ContainsKey ($Target )) {
6570 $Server = $servers [$Target ]
71+ if ($serverKeyFiles.ContainsKey ($Target )) {
72+ $keyFile = $serverKeyFiles [$Target ]
73+ }
6674} else {
6775 $Server = $Target
6876}
6977
70- # Get credentials
78+ # Get credentials directory
7179$credsDir = Join-Path $scriptDir " creds"
72- $credFile = $config.ssh.credentialFile
73- if (-not $credFile ) {
74- $credFile = " ssh-credentials.xml"
75- }
76- $credPath = Join-Path $credsDir $credFile
7780
78- if (-not (Test-Path $credPath )) {
79- Write-Host " "
80- Write-Host " Credential file not found!" - ForegroundColor Yellow
81- Write-Host " "
82- Write-Host " To set up SSH credentials:" - ForegroundColor Cyan
83- Write-Host " 1. Create credentials directory if needed:" - ForegroundColor White
84- Write-Host " New-Item -Path '$credsDir ' -ItemType Directory -Force" - ForegroundColor Gray
85- Write-Host " "
86- Write-Host " 2. Store your credentials:" - ForegroundColor White
87- Write-Host " `$ cred = Get-Credential -UserName 'your-ssh-username'" - ForegroundColor Gray
88- Write-Host " `$ cred | Export-Clixml '$credPath '" - ForegroundColor Gray
89- Write-Host " "
90- exit 1
81+ # Check for key file authentication
82+ $keyFilePath = $null
83+ if ($keyFile ) {
84+ $keyFilePath = Join-Path $credsDir $keyFile
85+ if (-not (Test-Path $keyFilePath )) {
86+ Write-Host " "
87+ Write-Host " Key file not found: $keyFilePath " - ForegroundColor Yellow
88+ Write-Host " "
89+ Write-Host " To set up SSH key authentication:" - ForegroundColor Cyan
90+ Write-Host " 1. Place your key file (.pem) in the creds directory:" - ForegroundColor White
91+ Write-Host " Copy-Item 'C:\path\to\your-key.pem' '$keyFilePath '" - ForegroundColor Gray
92+ Write-Host " "
93+ exit 1
94+ }
9195}
9296
93- $cred = Import-Clixml $credPath
94- $username = $cred.UserName
95- $password = $cred.GetNetworkCredential ().Password
97+ # Get password credentials (not needed if using key file)
98+ $username = $null
99+ $password = $null
100+ $cred = $null
101+
102+ if (-not $keyFile ) {
103+ $credFile = $config.ssh.credentialFile
104+ if (-not $credFile ) {
105+ $credFile = " ssh-credentials.xml"
106+ }
107+ $credPath = Join-Path $credsDir $credFile
108+
109+ if (-not (Test-Path $credPath )) {
110+ Write-Host " "
111+ Write-Host " Credential file not found!" - ForegroundColor Yellow
112+ Write-Host " "
113+ Write-Host " To set up SSH credentials:" - ForegroundColor Cyan
114+ Write-Host " 1. Create credentials directory if needed:" - ForegroundColor White
115+ Write-Host " New-Item -Path '$credsDir ' -ItemType Directory -Force" - ForegroundColor Gray
116+ Write-Host " "
117+ Write-Host " 2. Store your credentials:" - ForegroundColor White
118+ Write-Host " `$ cred = Get-Credential -UserName 'your-ssh-username'" - ForegroundColor Gray
119+ Write-Host " `$ cred | Export-Clixml '$credPath '" - ForegroundColor Gray
120+ Write-Host " "
121+ Write-Host " Or configure a key file in config.json:" - ForegroundColor Cyan
122+ Write-Host ' "keyFile": "your-key.pem"' - ForegroundColor Gray
123+ Write-Host " "
124+ exit 1
125+ }
126+
127+ $cred = Import-Clixml $credPath
128+ $username = $cred.UserName
129+ $password = $cred.GetNetworkCredential ().Password
130+ } else {
131+ # For key file auth, we need a username from config or credential file
132+ $credFile = $config.ssh.credentialFile
133+ if ($credFile ) {
134+ $credPath = Join-Path $credsDir $credFile
135+ if (Test-Path $credPath ) {
136+ $cred = Import-Clixml $credPath
137+ $username = $cred.UserName
138+ }
139+ }
140+
141+ if (-not $username ) {
142+ Write-Host " "
143+ Write-Host " Username required for key file authentication." - ForegroundColor Yellow
144+ Write-Host " Create a credential file with just the username:" - ForegroundColor Cyan
145+ Write-Host " `$ cred = Get-Credential -UserName 'your-ssh-username'" - ForegroundColor Gray
146+ Write-Host " `$ cred | Export-Clixml '.\creds\ssh-credentials.xml'" - ForegroundColor Gray
147+ Write-Host " "
148+ exit 1
149+ }
150+ }
96151
97152Write-Host " Setting up SSH tunnel..." - ForegroundColor Cyan
98153Write-Host " Local: 127.0.0.1:$LocalPort " - ForegroundColor Green
@@ -104,20 +159,31 @@ Write-Host ""
104159# Use WSL with sshpass for the tunnel
105160$wsl = Get-Command wsl - ErrorAction SilentlyContinue
106161if ($wsl ) {
107- # Check if sshpass is installed
108- $hasSshpass = wsl bash - c " command -v sshpass >/dev/null 2>&1 && echo 'yes' || echo 'no'"
109- if ($hasSshpass -match ' no' ) {
110- Write-Host " Installing sshpass in WSL (one-time setup)..." - ForegroundColor Yellow
111- wsl bash - c " sudo apt-get update && sudo apt-get install -y sshpass"
162+ if ($keyFile ) {
163+ # Convert Windows path to WSL path for key file
164+ $wslKeyPath = wsl wslpath - u " '$keyFilePath '"
165+ # Create SSH tunnel using key file through WSL
166+ wsl bash - c " ssh -o StrictHostKeyChecking=no -i $wslKeyPath -N -L ${LocalPort} :${RemoteHost} :${RemotePort} $username @$Server "
167+ } else {
168+ # Check if sshpass is installed
169+ $hasSshpass = wsl bash - c " command -v sshpass >/dev/null 2>&1 && echo 'yes' || echo 'no'"
170+ if ($hasSshpass -match ' no' ) {
171+ Write-Host " Installing sshpass in WSL (one-time setup)..." - ForegroundColor Yellow
172+ wsl bash - c " sudo apt-get update && sudo apt-get install -y sshpass"
173+ }
174+
175+ # Create SSH tunnel using sshpass through WSL
176+ wsl bash - c " SSHPASS='$password ' sshpass -e ssh -o StrictHostKeyChecking=no -N -L ${LocalPort} :${RemoteHost} :${RemotePort} $username @$Server "
112177 }
113-
114- # Create SSH tunnel using sshpass through WSL
115- wsl bash - c " SSHPASS='$password ' sshpass -e ssh -o StrictHostKeyChecking=no -N -L ${LocalPort} :${RemoteHost} :${RemotePort} $username @$Server "
116178} else {
117179 # Fallback to Posh-SSH
118180 try {
119181 Import-Module Posh- SSH - ErrorAction Stop
120- $session = New-SSHSession - ComputerName $Server - Credential $cred - AcceptKey
182+ if ($keyFile ) {
183+ $session = New-SSHSession - ComputerName $Server - KeyFile $keyFilePath - AcceptKey
184+ } else {
185+ $session = New-SSHSession - ComputerName $Server - Credential $cred - AcceptKey
186+ }
121187
122188 Write-Host " Tunnel established. Keep this window open." - ForegroundColor Green
123189 Write-Host " Press Ctrl+C to close..." - ForegroundColor Yellow
0 commit comments