Connecting to port 21 with Paramiko and got paramiko.ssh_exception.SSHException: Error reading SSH protocol banner

Question:

I’m trying to connect to am SFTP server through Paramiko. I don’t have a host key. The following code is my attempt and it’s giving me an error that says:

paramiko.ssh_exception.SSHException: Error reading SSH protocol banner

I notice that port is usually 22 in other given examples, but the SFTP port I was given is 21. And when I tried 22, it gave me another error saying

Unable to connect to port 22

Thank you in advance for your guidance and insight. Please let me know if I could provide more information.

from paramiko.client import SSHClient
from paramiko import AutoAddPolicy

client = SSHClient()
client.set_missing_host_key_policy(AutoAddPolicy())

client.connect(hostname="a_private_ip",
               port=21,
               username="user",
               password="xxx")

sftp_handle = client.open_sftp()
Asked By: xiexieni9527

||

Answers:

Paramiko is an SFTP client. The SFTP uses port 22.

If you were given port 21, then it’s most likely NOT SFTP. The port 21 is used by FTP. Encrypted variant of FTP, called also FTPS, uses 21 too. And people sometimes mistake it for SFTP.

For FTP use FTP class from ftplib. For FTPS use FTP_TLS class from ftplib.

Answered By: Martin Prikryl
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.