Sample code for 30+ languages & platforms
DataFlex

Get SSH Server Authentication Methods

See more SSH Examples

Demonstrates getting the authentication methods advertised by an SSH server. Connect first but do not authenticate, then call GetAuthMethods, which returns a lowercase, comma-separated list such as publickey,password,keyboard-interactive.

Important: Querying the authentication methods intentionally disconnects from the server, so reconnect before authenticating.

Background: Knowing what a server accepts lets a client choose the right authentication path rather than guessing — for example, falling back to keyboard-interactive when password authentication is not offered, or failing fast with a clear message when only public-key is allowed. It is also useful diagnostically when authentication fails for reasons that are not obvious from the error text.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSsh
    Integer iPort
    String sAuthMethods
    String sPassword
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    //  Demonstrates getting the authentication methods advertised by an SSH server.

    Get Create (RefClass(cComChilkatSsh)) To hoSsh
    If (Not(IsComObjectCreated(hoSsh))) Begin
        Send CreateComObject of hoSsh
    End

    Move 22 To iPort

    //  The server must be connected, but not yet authenticated.
    Get ComConnect Of hoSsh "ssh.example.com" iPort To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Returns a lowercase, comma-separated list, such as:
    //      publickey,password,keyboard-interactive
    Get ComGetAuthMethods Of hoSsh To sAuthMethods
    Get ComLastMethodSuccess Of hoSsh To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Authentication methods supported by this server: " sAuthMethods

    //  IMPORTANT: Querying the authentication methods intentionally disconnects from the server.
    //  Reconnect before authenticating.
    Get ComConnect Of hoSsh "ssh.example.com" iPort To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Normally you would not hard-code the password in source.  You should instead obtain it
    //  from an interactive prompt, environment variable, or a secrets vault.
    Move "mySshPassword" To sPassword

    Get ComAuthenticatePw Of hoSsh "mySshLogin" sPassword To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "SSH authentication successful."

    Send ComDisconnect To hoSsh


End_Procedure