Sample code for 30+ languages & platforms
PowerBuilder

Get SSH Server Authentication Methods

See more SSH Examples

Demonstrates getting the authentication methods advertised by an SSH server. Connect first but do not authenticate, then call GetAuthMethods, which returns a lowercase, comma-separated list such as publickey,password,keyboard-interactive.

Important: Querying the authentication methods intentionally disconnects from the server, so reconnect before authenticating.

Background: Knowing what a server accepts lets a client choose the right authentication path rather than guessing — for example, falling back to keyboard-interactive when password authentication is not offered, or failing fast with a clear message when only public-key is allowed. It is also useful diagnostically when authentication fails for reasons that are not obvious from the error text.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Ssh
integer li_Port
string ls_AuthMethods
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 getting the authentication methods advertised by an SSH server.

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

li_Port = 22

//  The server must be connected, but not yet authenticated.
li_Success = loo_Ssh.Connect("ssh.example.com",li_Port)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  Returns a lowercase, comma-separated list, such as:
//      publickey,password,keyboard-interactive
ls_AuthMethods = loo_Ssh.GetAuthMethods()
if loo_Ssh.LastMethodSuccess = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

Write-Debug "Authentication methods supported by this server: " + ls_AuthMethods

//  IMPORTANT: Querying the authentication methods intentionally disconnects from the server.
//  Reconnect before authenticating.
li_Success = loo_Ssh.Connect("ssh.example.com",li_Port)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    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"

li_Success = loo_Ssh.AuthenticatePw("mySshLogin",ls_Password)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

Write-Debug "SSH authentication successful."

loo_Ssh.Disconnect()


destroy loo_Ssh