Sample code for 30+ languages & platforms
PowerBuilder

Use a PuTTY Key for SSH Authentication

See more SSH Examples

Demonstrates authenticating with an SSH server using a username and a PuTTY (.ppk) private key. The key is imported with FromPuttyPrivateKey — setting Password first if it is encrypted — and passed to AuthenticatePk.

Note: The file paths are relative to the application's current working directory. Supply the paths to your own files.

Background: A PuTTY key can be used directly for authentication with no conversion step, which is convenient when the key was generated by PuTTYgen on Windows. Conceptually the private key takes the place of a password while the username identifies the account, and the corresponding public key must already be installed on the server. Convert to OpenSSH format only if some other tool in your pipeline requires PEM.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Ssh
oleobject loo_PuttyKey
string ls_PpkText
string ls_SshHostname
integer li_SshPort

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 a username and a PuTTY (.ppk) private
//  key.  The private key serves as the credential; the username identifies the account on the
//  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

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

ls_PpkText = loo_PuttyKey.LoadText("qa_data/ppk/putty_private_secret.ppk")
if loo_PuttyKey.LastMethodSuccess = 0 then
    Write-Debug loo_PuttyKey.LastErrorText
    destroy loo_Ssh
    destroy loo_PuttyKey
    return
end if

//  Set the password before importing an encrypted .ppk.  This should come from a secure source
//  rather than being hard-coded.
loo_PuttyKey.Password = "myKeyPassword"

li_Success = loo_PuttyKey.FromPuttyPrivateKey(ls_PpkText)
if li_Success = 0 then
    Write-Debug loo_PuttyKey.LastErrorText
    destroy loo_Ssh
    destroy loo_PuttyKey
    return
end if

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

//  The corresponding public key must already be installed on the SSH server for the account.
li_Success = loo_Ssh.AuthenticatePk("mySshLogin",loo_PuttyKey)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    destroy loo_PuttyKey
    return
end if

Write-Debug "Connection and authentication with the SSH server completed."

loo_Ssh.Disconnect()


destroy loo_Ssh
destroy loo_PuttyKey