Sample code for 30+ languages & platforms
Perl Requires Chilkat v11.0.0+

SFTP Authentication using an SSH Certificate

See more SFTP Examples

Demonstrates how to SFTP authenticate using an SSH certificate.

Chilkat Perl Downloads

Chilkat2 Perl
use strict;
use warnings;
use Chilkat2;

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

my $sbSshCert = Chilkat2::StringBuilder->new;
my $success = $sbSshCert->LoadFile('qa_data/sshCert/user_ecdsa_key-cert.pub', 'utf-8');
if (!$success) {
    print "Failed to load user_ecdsa_key-cert.pub\n";
    exit;
}

my $sbPrivKey = Chilkat2::StringBuilder->new;
$success = $sbPrivKey->LoadFile('qa_data/sshKeys/user_ecdsa_key', 'utf-8');
if (!$success) {
    print "Failed to load user_ecdsa_key\n";
    exit;
}

my $key = Chilkat2::SshKey->new;
# Provide the password if the user_ecdsa_key is stored in an encrypted format.
$key->Password('secret');
$success = $key->FromOpenSshPrivateKey($sbPrivKey->GetAsString());
if (!$success) {
    print $key->LastErrorText, "\n";
    exit;
}

# Indicate that the SSH certificate is to be used for authentication.
$key->UseSshCertificate($sbSshCert->GetAsString());

my $sftp = Chilkat2::SFtp->new;

my $hostname = 'sftp.example.com';
my $port = 22;
$success = $sftp->Connect($hostname, $port);
if (!$success) {
    print $sftp->LastErrorText, "\n";
    exit;
}

$success = $sftp->AuthenticatePk('myLogin', $key);
if (!$success) {
    print $sftp->LastErrorText, "\n";
    exit;
}

print "Public-Key Authentication using an SSH Certificate was Successful!\n";