Sample code for 30+ languages & platforms
PowerBuilder

Authenticate an SSH Tunnel with a Private Key

See more POP3 Examples

Demonstrates the Chilkat MailMan.SshAuthenticatePk method, which authenticates with the SSH server using public-key authentication after an SSH tunnel has been opened. The first argument is the SSH account name and the second is the SSH private key (an SshKey object). This example opens a tunnel, loads an OpenSSH private key, authenticates with it, and runs a POP3 operation through the tunnel.

Background: SSH public-key authentication replaces a password with a key pair: the public key is installed on the SSH server, and the client proves it holds the matching private key without ever transmitting a secret. This is the preferred method for automated systems — there is no password to embed or rotate, and the key can be revoked server-side. Chilkat reads the key with an SshKey object, which understands common formats such as OpenSSH and PuTTY.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Mailman
oleobject loo_SshKey
string ls_KeyText
integer li_Count

li_Success = 0

//  Demonstrates the MailMan.SshAuthenticatePk method, which authenticates with the SSH server
//  using public-key authentication after an SSH tunnel has been opened.  The 1st argument is
//  the SSH account name and the 2nd is the SSH private key.

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

//  Configure the POP3 server connection.  These connections will travel through the tunnel.
loo_Mailman.MailHost = "pop.example.com"
loo_Mailman.MailPort = 995
loo_Mailman.PopSsl = 1
loo_Mailman.PopUsername = "user@example.com"
loo_Mailman.PopPassword = "myPassword"

//  Open the SSH tunnel first.
li_Success = loo_Mailman.SshOpenTunnel("ssh.example.com",22)
if li_Success = 0 then
    Write-Debug loo_Mailman.LastErrorText
    destroy loo_Mailman
    return
end if

//  Load the SSH private key from an OpenSSH-format key file.
loo_SshKey = create oleobject
li_rc = loo_SshKey.ConnectToNewObject("Chilkat.SshKey")

ls_KeyText = loo_SshKey.LoadText("qa_data/ssh/id_rsa")
if loo_SshKey.LastMethodSuccess = 0 then
    Write-Debug loo_SshKey.LastErrorText
    destroy loo_Mailman
    destroy loo_SshKey
    return
end if

li_Success = loo_SshKey.FromOpenSshPrivateKey(ls_KeyText)
if li_Success = 0 then
    Write-Debug loo_SshKey.LastErrorText
    destroy loo_Mailman
    destroy loo_SshKey
    return
end if

//  Authenticate with the SSH server using the private key.
li_Success = loo_Mailman.SshAuthenticatePk("sshUser",loo_SshKey)
if li_Success = 0 then
    Write-Debug loo_Mailman.LastErrorText
    destroy loo_Mailman
    destroy loo_SshKey
    return
end if

//  POP3 operations now travel through the SSH tunnel.
li_Count = loo_Mailman.GetMailboxCount()
if li_Count < 0 then
    Write-Debug "Failed to get the mailbox count."
else
    Write-Debug "Mailbox count: " + string(li_Count)
end if

li_Success = loo_Mailman.SshCloseTunnel()
if li_Success = 0 then
    Write-Debug loo_Mailman.LastErrorText
    destroy loo_Mailman
    destroy loo_SshKey
    return
end if

//  Note: The path "qa_data/ssh/id_rsa" is a relative local filesystem path,
//  relative to the current working directory of the running application.


destroy loo_Mailman
destroy loo_SshKey