Sample code for 30+ languages & platforms
Chilkat2-Python

SSH Authentication using an SSH Certificate

See more SSH Examples

Demonstrates how to authenticate using an SSH certificate.

Chilkat Chilkat2-Python Downloads

Chilkat2-Python
import sys
import chilkat2

success = False

# This example assumes the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

sbSshCert = chilkat2.StringBuilder()
success = sbSshCert.LoadFile("qa_data/sshCert/user_ecdsa_key-cert.pub","utf-8")
if (success == False):
    print("Failed to load user_ecdsa_key-cert.pub")
    sys.exit()

sbPrivKey = chilkat2.StringBuilder()
success = sbPrivKey.LoadFile("qa_data/sshKeys/user_ecdsa_key","utf-8")
if (success == False):
    print("Failed to load user_ecdsa_key")
    sys.exit()

key = chilkat2.SshKey()
# Provide the password if the user_ecdsa_key is stored in an encrypted format.
key.Password = "secret"
success = key.FromOpenSshPrivateKey(sbPrivKey.GetAsString())
if (success == False):
    print(key.LastErrorText)
    sys.exit()

# Indicate that the SSH certificate is to be used for authentication.
# The UseSshCertificate method was added in Chilkat v11.0.0
key.UseSshCertificate(sbSshCert.GetAsString())

ssh = chilkat2.Ssh()

hostname = "ssh.example.com"
port = 22
success = ssh.Connect(hostname,port)
if (success != True):
    print(ssh.LastErrorText)
    sys.exit()

success = ssh.AuthenticatePk("myLogin",key)
if (success != True):
    print(ssh.LastErrorText)
    sys.exit()

print("Public-Key Authentication using an SSH Certificate was Successful!")