PowerBuilder
PowerBuilder
SSH Keyboard Authentication
See more SSH Examples
Demonstrates how to implement keyboard authentication with an SSH server.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Ssh
string ls_Hostname
integer li_Port
string ls_XmlResponse
oleobject loo_Xml
li_Success = 0
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
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
// Set some timeouts, in milliseconds:
loo_Ssh.ConnectTimeoutMs = 5000
loo_Ssh.IdleTimeoutMs = 15000
// Connect to the SSH server.
// The standard SSH port = 22
// The hostname may be a hostname or IP address.
ls_Hostname = "sftp.example.com"
li_Port = 22
li_Success = loo_Ssh.Connect(ls_Hostname,li_Port)
if li_Success <> 1 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
return
end if
// Begin keyboard authentication..
ls_XmlResponse = loo_Ssh.StartKeyboardAuth("myLogin")
if loo_Ssh.LastMethodSuccess <> 1 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
return
end if
// If a user authentication banner was received, then your app
// may display it prior to prompting for the password.
Write-Debug "UserAuthBanner: " + loo_Ssh.UserAuthBanner
loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")
li_Success = loo_Xml.LoadXml(ls_XmlResponse)
// Assume LoadXml succeeds for the example..
if loo_Xml.HasChildWithTag("success") = 1 then
Write-Debug "No password required, already authenticated."
destroy loo_Ssh
destroy loo_Xml
return
end if
if loo_Xml.HasChildWithTag("error") = 1 then
Write-Debug "Authentication already failed."
destroy loo_Ssh
destroy loo_Xml
return
end if
// See the online reference documentation for Chilkat SSH.
// The XML returned by StartKeyboardAuth will contain an infoRequest
// with one or more prompts that your application may choose to display.
// Call ContinueKeyboardAuth, passing in the whatever information is requires (such as the password).
// Typically, keyboard authentication requires one call to ContinueKeyboardAuth
// using the password. Theoretically, the SSH server could prompt for additional pieces
// of information. The authentication is completed when the XML returned contains
// either a "success" or "error" child node.
// This example asumes only one call to ContinueKeyboardAuth is required.
ls_XmlResponse = loo_Ssh.ContinueKeyboardAuth("myPassword")
if loo_Ssh.LastMethodSuccess <> 1 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
destroy loo_Xml
return
end if
li_Success = loo_Xml.LoadXml(ls_XmlResponse)
// Assume LoadXml succeeds for the example..
if loo_Xml.HasChildWithTag("success") = 1 then
Write-Debug "SSH Keyboard Authentication Successful!"
destroy loo_Ssh
destroy loo_Xml
return
end if
if loo_Xml.HasChildWithTag("error") = 1 then
Write-Debug "Authentication failed."
destroy loo_Ssh
destroy loo_Xml
return
end if
destroy loo_Ssh
destroy loo_Xml