Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oTunnel
LOCAL nSshPort
LOCAL cInfoRequestXml
LOCAL nWaitForThreadExit

nSuccess := 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.

oTunnel := CreateObject("Chilkat.SshTunnel")

//  The tunnel forwards accepted local connections to this destination through the SSH server.
oTunnel:DestHostname := "db.internal.example.com"
oTunnel:DestPort := 5432

//  Connect to the SSH server.  This performs the TCP/proxy connection and SSH handshake, but
//  does not authenticate yet.
nSshPort := 22
nSuccess := oTunnel:Connect("ssh.example.com", nSshPort)
IF (nSuccess == 0)
    ? oTunnel:LastErrorText
    oTunnel:destroy()
    RETURN
ENDIF

//  Begin keyboard-interactive authentication.  The returned XML describes the server's prompts.
cInfoRequestXml := oTunnel:StartKeyboardAuth("mySshLogin")
IF (oTunnel:LastMethodSuccess == 0)
    ? oTunnel:LastErrorText
    oTunnel:destroy()
    RETURN
ENDIF

? cInfoRequestXml

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

nWaitForThreadExit := 1
nSuccess := oTunnel:CloseTunnel(nWaitForThreadExit)
IF (nSuccess == 0)
    ? oTunnel:LastErrorText
    oTunnel:destroy()
    RETURN
ENDIF

oTunnel:destroy()