Tcl
Tcl
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 Tcl Downloads
load ./chilkat.dll
set 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.
set mailman [new_CkMailMan]
# Configure the POP3 server connection. These connections will travel through the tunnel.
CkMailMan_put_MailHost $mailman "pop.example.com"
CkMailMan_put_MailPort $mailman 995
CkMailMan_put_PopSsl $mailman 1
CkMailMan_put_PopUsername $mailman "user@example.com"
CkMailMan_put_PopPassword $mailman "myPassword"
# Open the SSH tunnel first.
set success [CkMailMan_SshOpenTunnel $mailman "ssh.example.com" 22]
if {$success == 0} then {
puts [CkMailMan_lastErrorText $mailman]
delete_CkMailMan $mailman
exit
}
# Load the SSH private key from an OpenSSH-format key file.
set sshKey [new_CkSshKey]
set keyText [CkSshKey_loadText $sshKey "qa_data/ssh/id_rsa"]
if {[CkSshKey_get_LastMethodSuccess $sshKey] == 0} then {
puts [CkSshKey_lastErrorText $sshKey]
delete_CkMailMan $mailman
delete_CkSshKey $sshKey
exit
}
set success [CkSshKey_FromOpenSshPrivateKey $sshKey $keyText]
if {$success == 0} then {
puts [CkSshKey_lastErrorText $sshKey]
delete_CkMailMan $mailman
delete_CkSshKey $sshKey
exit
}
# Authenticate with the SSH server using the private key.
set success [CkMailMan_SshAuthenticatePk $mailman "sshUser" $sshKey]
if {$success == 0} then {
puts [CkMailMan_lastErrorText $mailman]
delete_CkMailMan $mailman
delete_CkSshKey $sshKey
exit
}
# POP3 operations now travel through the SSH tunnel.
set count [CkMailMan_GetMailboxCount $mailman]
if {$count < 0} then {
puts "Failed to get the mailbox count."
} else {
puts "Mailbox count: $count"
}
set success [CkMailMan_SshCloseTunnel $mailman]
if {$success == 0} then {
puts [CkMailMan_lastErrorText $mailman]
delete_CkMailMan $mailman
delete_CkSshKey $sshKey
exit
}
# Note: The path "qa_data/ssh/id_rsa" is a relative local filesystem path,
# relative to the current working directory of the running application.
delete_CkMailMan $mailman
delete_CkSshKey $sshKey