Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loMailman
LOCAL loSshKey
LOCAL lcKeyText
LOCAL lnCount

lnSuccess = 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.

loMailman = CreateObject('Chilkat.MailMan')

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

*  Open the SSH tunnel first.
lnSuccess = loMailman.SshOpenTunnel("ssh.example.com",22)
IF (lnSuccess = 0) THEN
    ? loMailman.LastErrorText
    RELEASE loMailman
    CANCEL
ENDIF

*  Load the SSH private key from an OpenSSH-format key file.
loSshKey = CreateObject('Chilkat.SshKey')
lcKeyText = loSshKey.LoadText("qa_data/ssh/id_rsa")
IF (loSshKey.LastMethodSuccess = 0) THEN
    ? loSshKey.LastErrorText
    RELEASE loMailman
    RELEASE loSshKey
    CANCEL
ENDIF

lnSuccess = loSshKey.FromOpenSshPrivateKey(lcKeyText)
IF (lnSuccess = 0) THEN
    ? loSshKey.LastErrorText
    RELEASE loMailman
    RELEASE loSshKey
    CANCEL
ENDIF

*  Authenticate with the SSH server using the private key.
lnSuccess = loMailman.SshAuthenticatePk("sshUser",loSshKey)
IF (lnSuccess = 0) THEN
    ? loMailman.LastErrorText
    RELEASE loMailman
    RELEASE loSshKey
    CANCEL
ENDIF

*  POP3 operations now travel through the SSH tunnel.
lnCount = loMailman.GetMailboxCount()
IF (lnCount < 0) THEN
    ? "Failed to get the mailbox count."
ELSE
    ? "Mailbox count: " + STR(lnCount)
ENDIF

lnSuccess = loMailman.SshCloseTunnel()
IF (lnSuccess = 0) THEN
    ? loMailman.LastErrorText
    RELEASE loMailman
    RELEASE loSshKey
    CANCEL
ENDIF

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

RELEASE loMailman
RELEASE loSshKey