Sample code for 30+ languages & platforms
PowerBuilder

SSH Public Key Authentication

See more SSH Examples

Demonstrates authenticating with an SSH server using public-key authentication. A private key is loaded into an SshKey object and passed to AuthenticatePk. The matching public key must already be installed on the server for the account.

Note: The private-key path is relative to the application's current working directory. Supply the path to your own key file.

Background: Public-key authentication is the standard for unattended and automated SSH: the private key never leaves the client, the server merely trusts the corresponding public key, and no reusable password crosses the wire. SshKey imports several formats — OpenSSH PEM keys via FromOpenSshPrivateKey and PuTTY .ppk files via FromPuttyPrivateKey — encrypted or not. For an encrypted key, set the Password property before importing. Note that LoadText is only a convenience for reading a text file into a string; it does not itself load the key.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Ssh
string ls_Hostname
integer li_Port
oleobject loo_Key
string ls_PrivKeyText

li_Success = 0

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

//  Demonstrates authenticating with an SSH server using public-key authentication.  The matching
//  public key must already be installed on the SSH server for the account.

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

//  Timeouts, in milliseconds.
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

//  LoadText is a convenience method that reads any text file into a string.  It does not itself
//  load the key.
loo_Key = create oleobject
li_rc = loo_Key.ConnectToNewObject("Chilkat.SshKey")

ls_PrivKeyText = loo_Key.LoadText("qa_data/my_private_key.pem")
if loo_Key.LastMethodSuccess = 0 then
    Write-Debug loo_Key.LastErrorText
    destroy loo_Ssh
    destroy loo_Key
    return
end if

//  Import the private key.  Both OpenSSH and PuTTY formats are supported, encrypted or not.
//  This example loads an unencrypted OpenSSH-format key; for a PuTTY .ppk file, call
//  FromPuttyPrivateKey instead.  For an encrypted key, set key.Password before importing.
li_Success = loo_Key.FromOpenSshPrivateKey(ls_PrivKeyText)
if li_Success = 0 then
    Write-Debug loo_Key.LastErrorText
    destroy loo_Ssh
    destroy loo_Key
    return
end if

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

Write-Debug "Public-key authentication successful."

loo_Ssh.Disconnect()


destroy loo_Ssh
destroy loo_Key