Sample code for 30+ languages & platforms
PowerBuilder

SSH Keyboard-Interactive Authentication

See more SSH Examples

Demonstrates keyboard-interactive authentication with an SSH server. StartKeyboardAuth returns XML describing the server's prompts, and ContinueKeyboardAuth submits each response. Authentication is complete when the returned XML contains either a success or an error node.

Background: Keyboard-interactive is SSH's flexible, prompt-driven method: rather than assuming a single password, the server asks one or more questions — a password, a one-time code, a security question — and the client answers each. This is how SSH supports two-factor and other challenge-response schemes. The prompt XML also indicates whether each response should be echoed, so a client knows when to mask input. A server may issue several rounds, so a robust implementation loops until it sees success or error rather than assuming one exchange is enough.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Ssh
string ls_Hostname
integer li_Port
string ls_XmlResponse
oleobject loo_Xml
string ls_Password

li_Success = 0

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

//  Demonstrates keyboard-interactive authentication with an SSH server.  The server sends one or
//  more prompts as XML, and the application answers each with ContinueKeyboardAuth.

loo_Ssh = create oleobject
li_rc = loo_Ssh.ConnectToNewObject("Chilkat.Ssh")
if li_rc < 0 then
    destroy loo_Ssh
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_Ssh.ConnectTimeoutMs = 5000
loo_Ssh.ReadTimeoutMs = 15000

ls_Hostname = "ssh.example.com"
li_Port = 22
li_Success = loo_Ssh.Connect(ls_Hostname,li_Port)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  Begin keyboard-interactive authentication.  The returned XML describes the server's prompts.
ls_XmlResponse = loo_Ssh.StartKeyboardAuth("mySshLogin")
if loo_Ssh.LastMethodSuccess = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  If the server sent a user authentication banner, an application may display it before
//  prompting.
Write-Debug "UserAuthBanner: " + loo_Ssh.UserAuthBanner

loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")

li_Success = loo_Xml.LoadXml(ls_XmlResponse)
if li_Success = 0 then
    Write-Debug loo_Xml.LastErrorText
    destroy loo_Ssh
    destroy loo_Xml
    return
end if

//  Authentication is complete when the XML contains either a "success" or an "error" node.
if loo_Xml.HasChildWithTag("success") then
    Write-Debug "No password required, already authenticated."
    destroy loo_Ssh
    destroy loo_Xml
    return
end if

if loo_Xml.HasChildWithTag("error") then
    Write-Debug "Authentication failed."
    destroy loo_Ssh
    destroy loo_Xml
    return
end if

//  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.
ls_Password = "mySshPassword"

//  Answer the prompt.  Typically one call is enough, but a server may issue several rounds of
//  prompts, so a robust client loops until it sees "success" or "error".
ls_XmlResponse = loo_Ssh.ContinueKeyboardAuth(ls_Password)
if loo_Ssh.LastMethodSuccess = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    destroy loo_Xml
    return
end if

li_Success = loo_Xml.LoadXml(ls_XmlResponse)
if li_Success = 0 then
    Write-Debug loo_Xml.LastErrorText
    destroy loo_Ssh
    destroy loo_Xml
    return
end if

if loo_Xml.HasChildWithTag("success") then
    Write-Debug "SSH keyboard-interactive authentication successful."
    destroy loo_Ssh
    destroy loo_Xml
    return
end if

if loo_Xml.HasChildWithTag("error") then
    Write-Debug "Authentication failed."
end if



destroy loo_Ssh
destroy loo_Xml