DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoMailman
Variant vSshKey
Handle hoSshKey
String sKeyText
Integer iCount
String sTemp1
Boolean bTemp1
Move False To iSuccess
// 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.
Get Create (RefClass(cComChilkatMailMan)) To hoMailman
If (Not(IsComObjectCreated(hoMailman))) Begin
Send CreateComObject of hoMailman
End
// Configure the POP3 server connection. These connections will travel through the tunnel.
Set ComMailHost Of hoMailman To "pop.example.com"
Set ComMailPort Of hoMailman To 995
Set ComPopSsl Of hoMailman To True
Set ComPopUsername Of hoMailman To "user@example.com"
Set ComPopPassword Of hoMailman To "myPassword"
// Open the SSH tunnel first.
Get ComSshOpenTunnel Of hoMailman "ssh.example.com" 22 To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoMailman To sTemp1
Showln sTemp1
Procedure_Return
End
// Load the SSH private key from an OpenSSH-format key file.
Get Create (RefClass(cComChilkatSshKey)) To hoSshKey
If (Not(IsComObjectCreated(hoSshKey))) Begin
Send CreateComObject of hoSshKey
End
Get ComLoadText Of hoSshKey "qa_data/ssh/id_rsa" To sKeyText
Get ComLastMethodSuccess Of hoSshKey To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoSshKey To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComFromOpenSshPrivateKey Of hoSshKey sKeyText To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSshKey To sTemp1
Showln sTemp1
Procedure_Return
End
// Authenticate with the SSH server using the private key.
Get pvComObject of hoSshKey to vSshKey
Get ComSshAuthenticatePk Of hoMailman "sshUser" vSshKey To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoMailman To sTemp1
Showln sTemp1
Procedure_Return
End
// POP3 operations now travel through the SSH tunnel.
Get ComGetMailboxCount Of hoMailman To iCount
If (iCount < 0) Begin
Showln "Failed to get the mailbox count."
End
Else Begin
Showln "Mailbox count: " iCount
End
Get ComSshCloseTunnel Of hoMailman To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoMailman To sTemp1
Showln sTemp1
Procedure_Return
End
// Note: The path "qa_data/ssh/id_rsa" is a relative local filesystem path,
// relative to the current working directory of the running application.
End_Procedure