Sample code for 30+ languages & platforms
PureBasic

Get the SSH Server's Authentication Methods

See more SSH Examples

Demonstrates the Chilkat Ssh.GetAuthMethods method, which queries a connected but unauthenticated SSH server for the authentication methods it advertises. It takes no arguments and returns a lowercase, comma-separated list such as publickey,password,keyboard-interactive. Call it after Connect and before authenticating.

Background: Knowing what the server accepts lets a client choose the right authentication path — for example, falling back to keyboard-interactive when password auth is not offered. Note the documented connection side effect: querying methods advances the SSH authentication state, so call it at the correct point in the sequence.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkSsh.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the Ssh.GetAuthMethods method, which queries a connected but unauthenticated SSH
    ;  server for the authentication methods it advertises.  It takes no arguments and returns a
    ;  lowercase, comma-separated list such as "publickey,password,keyboard-interactive".

    ssh.i = CkSsh::ckCreate()
    If ssh.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Connect to the SSH server.  Port 22 is the usual SSH port.
    sshPort.i = 22
    success = CkSsh::ckConnect(ssh,"ssh.example.com",sshPort)
    If success = 0
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    ;  Query the advertised authentication methods.  Call after Connect, before authenticating.
    methods.s = CkSsh::ckGetAuthMethods(ssh)
    If CkSsh::ckLastMethodSuccess(ssh) = 0
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    Debug "Server auth methods: " + methods

    CkSsh::ckDisconnect(ssh)


    CkSsh::ckDispose(ssh)


    ProcedureReturn
EndProcedure