Sample code for 30+ languages & platforms
Tcl

Authenticate an SSH Tunnel with a Private Key

See more Socket/SSL/TLS Examples

Demonstrates Socket.SshAuthenticatePk, which authenticates an SSH tunnel session using a private key loaded into an SshKey object. Call SshOpenTunnel first.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. The SSH server must have the corresponding public key authorized for the account. If the private key is encrypted, set the SshKey.Password property before loading it.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

set socket [new_CkSocket]

set success [CkSocket_SshOpenTunnel $socket "ssh.example.com" 22]
if {$success == 0} then {
    puts [CkSocket_lastErrorText $socket]
    delete_CkSocket $socket
    exit
}

#  Load the SSH private key.  The file path is relative to the application's current working
#  directory.  If the key is encrypted, set the SshKey.Password property before calling
#  FromOpenSshPrivateKey.
set sshKey [new_CkSshKey]

set keyText [CkSshKey_loadText $sshKey "qa_data/id_ed25519"]
if {[CkSshKey_get_LastMethodSuccess $sshKey] == 0} then {
    puts [CkSshKey_lastErrorText $sshKey]
    delete_CkSocket $socket
    delete_CkSshKey $sshKey
    exit
}

set success [CkSshKey_FromOpenSshPrivateKey $sshKey $keyText]
if {$success == 0} then {
    puts [CkSshKey_lastErrorText $sshKey]
    delete_CkSocket $socket
    delete_CkSshKey $sshKey
    exit
}

#  Authenticate the SSH tunnel session using public-key authentication.  The SSH server must have the
#  corresponding public key authorized for the account.
set success [CkSocket_SshAuthenticatePk $socket "sshUser" $sshKey]
if {$success == 0} then {
    puts [CkSocket_lastErrorText $socket]
    delete_CkSocket $socket
    delete_CkSshKey $sshKey
    exit
}

puts "SSH public-key authentication succeeded."

delete_CkSocket $socket
delete_CkSshKey $sshKey