Sample code for 30+ languages & platforms
PureBasic

Start SSH Tunnel Keyboard-Interactive Authentication

See more SSH Tunnel Examples

Demonstrates the Chilkat SshTunnel.StartKeyboardAuth method, which begins keyboard-interactive authentication. The only argument is the login name. It returns XML describing the server's prompts, which are answered with ContinueKeyboardAuth.

Background: Keyboard-interactive is the prompt-driven authentication method: the server sends one or more questions — a password, a one-time code, a security question — and the client answers each. This is how a tunnel supports two-factor and other challenge-response schemes. The returned XML says exactly what to ask and whether to mask the input.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkSshTunnel.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the SshTunnel.StartKeyboardAuth method, which begins keyboard-interactive
    ;  authentication.  The only argument is the login name.  It returns XML describing the server's
    ;  prompts, which are answered with ContinueKeyboardAuth.

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

    ;  The tunnel forwards accepted local connections to this destination through the SSH server.
    CkSshTunnel::setCkDestHostname(tunnel, "db.internal.example.com")
    CkSshTunnel::setCkDestPort(tunnel, 5432)

    ;  Connect to the SSH server.  This performs the TCP/proxy connection and SSH handshake, but
    ;  does not authenticate yet.
    sshPort.i = 22
    success = CkSshTunnel::ckConnect(tunnel,"ssh.example.com",sshPort)
    If success = 0
        Debug CkSshTunnel::ckLastErrorText(tunnel)
        CkSshTunnel::ckDispose(tunnel)
        ProcedureReturn
    EndIf

    ;  Begin keyboard-interactive authentication.  The returned XML describes the server's prompts.
    infoRequestXml.s = CkSshTunnel::ckStartKeyboardAuth(tunnel,"mySshLogin")
    If CkSshTunnel::ckLastMethodSuccess(tunnel) = 0
        Debug CkSshTunnel::ckLastErrorText(tunnel)
        CkSshTunnel::ckDispose(tunnel)
        ProcedureReturn
    EndIf

    Debug infoRequestXml

    ;  Answer the prompt(s) with ContinueKeyboardAuth, then call BeginAccepting.

    waitForThreadExit.i = 1
    success = CkSshTunnel::ckCloseTunnel(tunnel,waitForThreadExit)
    If success = 0
        Debug CkSshTunnel::ckLastErrorText(tunnel)
        CkSshTunnel::ckDispose(tunnel)
        ProcedureReturn
    EndIf



    CkSshTunnel::ckDispose(tunnel)


    ProcedureReturn
EndProcedure