Created
June 6, 2023 09:52
-
-
Save Shotokhan/0084590ae7f845ba1a19eaed92dc2539 to your computer and use it in GitHub Desktop.
List user@host SSH saved profiles in Windows SSH config for current user
This file contains hidden or 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
$inputData = Get-Content "C:\Users\$Env:UserName\.ssh\config" | |
$pattern = 'User\s+([^\s]+)|Host\s+([^\s]+)' | |
$reg_matches = [regex]::Matches($inputData, $pattern, 'Multiline') | |
$hosts = @{} | |
$currentHost = "" | |
foreach ($match in $reg_matches) { | |
if ($match.Groups[1].Success) { | |
$user = $match.Groups[1].Value | |
$hosts[$currentHost] = $user | |
} elseif ($match.Groups[2].Success) { | |
$currentHost = $match.Groups[2].Value | |
} | |
} | |
foreach ($ssh_host in $hosts.GetEnumerator()) { | |
$output = "{0}@{1}" -f $ssh_host.Value, $ssh_host.Name | |
Write-Output $output | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment