Skip to content

SSH.KnownHosts

Andrew Lambert edited this page Apr 22, 2023 · 15 revisions

SSH.KnownHosts

Class Declaration

 Protected Class KnownHosts

Remarks

This class represents a list of known SSH servers. When you connect to a server you can use this class to verify that the server fingerprint matches the one stored in the list. If the fingerprint doesn't match then you should show a big scary warning to the user and bail out. If the server isn't found in the list then you can add it.

Verifying the server's fingerprint is optional, but strongly recommended, and should be done before sending the user's credentials.

Methods

Properties

Example

This example connects to the remote server and then compares its fingerprint to a list of known hosts loaded from the user's home folder.

  Dim session As New SSH.Session()
  If Not session.Connect("ssh.example.com", 22) Then MsgBox("Unable to connect!")
  
  ' locate the user's known_hosts file (or supply your own)
  Dim f As FolderItem = SpecialFolder.UserHome.Child(".ssh")
  If f.Exists Then f = f.Child("known_hosts")
  If f.Exists Then
    Dim known As New SSH.KnownHosts(session)
    Call known.Load(f)
    
    If Not session.CheckHost(known, False) Then
      If session.LastError = SSH.ERR_HOSTKEY_NOTFOUND Then
        Call MsgBox("Fingerprint not known!", 16, "Unknown server")
        Return
        
      ElseIf session.LastError = SSH.ERR_HOSTKEY_MISMATCH Then
        Call MsgBox("Fingerprint has changed!", 16, "Security breach")
        Return
        
      ElseIf session.LastError <> 0 Then
        Call MsgBox("Unable to verify fingerprint.", 16, "Unknown error")
        Return
        
      End If
    End If
  End If
    
  ' proceed with the session by sending the credentials
Clone this wiki locally