Skip to content

Instantly share code, notes, and snippets.

@MVKozlov
Forked from RulerOf/get-sshfingerprint.ps1
Created July 26, 2022 08:27
Show Gist options
  • Save MVKozlov/d7bbbde2851db6a3f16974ff9c468671 to your computer and use it in GitHub Desktop.
Save MVKozlov/d7bbbde2851db6a3f16974ff9c468671 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