Chilkat2-Python
Chilkat2-Python
SFTP Authentication using an SSH Certificate
See more SFTP Examples
Demonstrates how to SFTP authenticate using an SSH certificate.Chilkat Chilkat2-Python Downloads
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())
sftp = chilkat2.SFtp()
hostname = "sftp.example.com"
port = 22
success = sftp.Connect(hostname,port)
if (success != True):
print(sftp.LastErrorText)
sys.exit()
success = sftp.AuthenticatePk("myLogin",key)
if (success != True):
print(sftp.LastErrorText)
sys.exit()
print("Public-Key Authentication using an SSH Certificate was Successful!")