Sample code for 30+ languages & platforms
PowerBuilder

Authenticate an IMAP SSH Tunnel with a Private Key

See more IMAP Examples

Demonstrates the Chilkat Imap.SshAuthenticatePk method, which authenticates the SSH tunnel using a username and an SshKey private key. The first argument is the SSH login name and the second is the private key. Call SshOpenTunnel first; the matching public key must already be authorized for the login on the SSH server.

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 usual choice for unattended/automated SSH access: the private key stays on the client and the server is configured to trust the corresponding public key, so no reusable password travels over the wire. The SshKey object can import keys in several formats — this example uses an OpenSSH private key.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Imap
integer li_SshPort
oleobject loo_PrivKey
string ls_KeyStr

li_Success = 0

//  Demonstrates the Imap.SshAuthenticatePk method, which authenticates the SSH tunnel using a
//  username and a private key.  The 1st argument is the SSH login name and the 2nd is an SshKey
//  private key.  Call SshOpenTunnel first.

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

li_SshPort = 22
li_Success = loo_Imap.SshOpenTunnel("ssh.example.com",li_SshPort)
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

//  Load the SSH private key.  LoadText returns the file's text, which is then imported.
loo_PrivKey = create oleobject
li_rc = loo_PrivKey.ConnectToNewObject("Chilkat.SshKey")

ls_KeyStr = loo_PrivKey.LoadText("qa_data/id_rsa")
if loo_PrivKey.LastMethodSuccess = 0 then
    Write-Debug loo_PrivKey.LastErrorText
    destroy loo_Imap
    destroy loo_PrivKey
    return
end if

li_Success = loo_PrivKey.FromOpenSshPrivateKey(ls_KeyStr)
if li_Success = 0 then
    Write-Debug loo_PrivKey.LastErrorText
    destroy loo_Imap
    destroy loo_PrivKey
    return
end if

//  Authenticate the SSH tunnel with the private key.  The matching public key must already be
//  authorized for the login on the SSH server.
li_Success = loo_Imap.SshAuthenticatePk("sshUser",loo_PrivKey)
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_PrivKey
    return
end if

//  With the SSH tunnel in place, connect and log in to the IMAP server as usual.  The IMAP
//  traffic flows through the tunnel automatically.
loo_Imap.Ssl = 1
loo_Imap.Port = 993
li_Success = loo_Imap.Connect("imap.example.com")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_PrivKey
    return
end if

li_Success = loo_Imap.Login("user@example.com","myPassword")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_PrivKey
    return
end if

li_Success = loo_Imap.Disconnect()
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_PrivKey
    return
end if

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



destroy loo_Imap
destroy loo_PrivKey