How to use pysftp with the -o"HostKeyAlgorithms=+ssh-rsa" -o"PubkeyAcceptedKeyTypes=+ssh-rsa" options?

37 Views Asked by At

Using paramiko I want to select the list of pubkey algorithm my key is in, do you know how I can run a similar command like:

sftp \
    -i private_key.pem \
    -P 22 \
    -o"HostKeyAlgorithms=+ssh-rsa" \
    -o"PubkeyAcceptedKeyTypes=+ssh-rsa" \
    [email protected]
1

There are 1 best solutions below

0
Natim On

After some trouble, we were able to figure out the issue:

import paramiko
import pysftp

hostname = 'ftp.acme.org'
port = 22
username = 'username'
private_key_path = 'private_key.pem'

paramiko.transport.Transport._preferred_pubkeys = ('ssh-rsa',)

# SECURITY BREACH: ONLY USE FOR DEBUGGING PURPOSES
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None


with pysftp.Connection(
    host=hostname,
    port=port,
    username=username,
    private_key=private_key_path,
    cnopts=cnopts,
) as sftp:
    files = sftp.listdir()
    print(files)

Mind you, it is a bad security usage to set cnopts.hostkeys = None but this is another story, I use it so that you don't reach other issues while trying to fix the pubkey one.