Last active
October 20, 2023 11:50
-
-
Save dhcgn/b48df4524e4684fbf23c3aa27042fe5a to your computer and use it in GitHub Desktop.
Some PowerShell scripts for ssh for linux and windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.Description | |
Get the SSH Config folder in sync by copying all files from /mnt/c/Users/%USERNAME%/.ssh/ to the wsl instance | |
Call this script from the wsl instance | |
#> | |
$windowsUser = cmd.exe /c "echo %USERNAME%" 2>$null | |
if ($LASTEXITCODE -ne 0 -or $windowsUser -eq $null){ | |
Write-Error "Could not get Windows User" | |
return | |
} | |
Write-Host "Windows User: $windowsUser" | |
$source = '/mnt/c/Users/' + $windowsUser + '/.ssh/*' | |
$destination = '~/.ssh/' | |
# Check if source exists | |
if (-Not (Test-Path $source)){ | |
Write-Error "Source does not exist: " + $source | |
return | |
} | |
# Check if command dos2unix exists | |
if (-Not (Get-Command dos2unix -ErrorAction SilentlyContinue)){ | |
Write-Error "dos2unix is not installed" | |
return | |
} | |
Copy-Item -Path $source -Destination $destination -Exclude known_hosts* -Recurse -Verbose -Force | |
# Set permissions and line endings | |
Get-ChildItem -Path $destination | ForEach-Object { chmod 600 $_.FullName; dos2unix $_.FullName } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.DESCRIPTION | |
Test SSH logins for all hosts in ~/.ssh/config | |
#> | |
$file = "~/.ssh/config" | |
if (-not (Test-Path $file)) { | |
Write-Error "File $file does not exist" | |
return | |
} | |
$hosts = Select-String -Path $file -Pattern "Host (.*)" | ForEach-Object { $_.Matches.Groups[1].Value } | |
$hosts | ForEach-Object { | |
Write-Host "Testing: $_ " -NoNewline -ForegroundColor Magenta | |
ssh $_ uptime | |
if ($?) { | |
Write-Host "✅ $_ SSH connection is working" -ForegroundColor Green | |
} | |
else { | |
Write-Host "❌ $_ SSH connection is not working" -ForegroundColor Red | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment