Skip to content

Instantly share code, notes, and snippets.

@RulerOf
Created January 31, 2019 20:49
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save RulerOf/9da055720119d78defb7a5e5c69d10fb to your computer and use it in GitHub Desktop.
Save RulerOf/9da055720119d78defb7a5e5c69d10fb to your computer and use it in GitHub Desktop.
Get SSH host key fingerprint using PowerShell. Requires the WinSCP .Net assembly.

Description

You can use this snippet to retrieve an SSH host key fingerprint, suitable for usage with the winscp.com file transfer utility. You can copy/paste the function into your own script and use it that way.

Example

You can use it from the command line like this:

PS C:\Users\User\Desktop> function Get-SshFingerprint {
>>   param( [string]$ssh_server )
>>
>>   # Load WinSCP .NET assembly
>>   Add-Type -Path "${env:ProgramFiles(x86)}\WinSCP\WinSCPnet.dll"
>>
>>   # Setup session options
>>   $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
>>       Protocol = [WinSCP.Protocol]::Sftp
>>       HostName = $ssh_server
>>       UserName = ""
>>   }
>>
>>   # Scan for the host key
>>   $session = New-Object WinSCP.Session
>>   try
>>   {
>>       $fingerprint = $session.ScanFingerprint($sessionOptions, "SHA-256")
>>   }
>>   finally
>>   {
>>       $session.Dispose()
>>   }
>>
>>   # And output the host key to the pipeline
>>   Write-Output $fingerprint
>> }
PS C:\Users\User\Desktop> Get-SshFingerprint github.com
ssh-rsa 2048 nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8=
function Get-SshFingerprint {
param( [string]$ssh_server )
# Load WinSCP .NET assembly
Add-Type -Path "${env:ProgramFiles(x86)}\WinSCP\WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = $ssh_server
UserName = ""
}
# Scan for the host key
$session = New-Object WinSCP.Session
try
{
$fingerprint = $session.ScanFingerprint($sessionOptions, "SHA-256")
}
finally
{
$session.Dispose()
}
# And output the host key to the pipeline
Write-Output $fingerprint
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment