Skip to content

SSH.Session

Andrew Lambert edited this page Nov 26, 2022 · 23 revisions

SSH.Session

Class Declaration

 Protected Class Session

Remarks

This class represents a SSH session. A session is a TCP connection over which SSH traffic is sent and received. A given session can multiplex any number of Channels over its single TCP connection.

When you create a new instance of Session it is not connected. You must call Connect() to open the TCP connection and begin the SSH2 handshake. After the connection is established the user needs to log in. Before sending the user's credentials it is usually wise to compare the remote host's fingerprint to a list of known hosts.

Example

This example creates a new session, connects to the remote server, verifies the server's fingerprint against a list of known hosts, and finally authenticates to the server by sending a username and a password.

  Dim session As New SSH.Session()
  If Not session.Connect("ssh.example.com", 22) Then
    MsgBox("Unable to connect!")
    Return
  End If
  
  ' 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
  
  If Not session.SendCredentials("username", "password") Then
    MsgBox("Invalid user/pass!")
    Return
  End If

Methods

Properties

Clone this wiki locally