Menu Close

How to verify host key with Python pysftp?

Sometimes, we want to verify host key with Python pysftp.

In this article, we’ll look at how to verify host key with Python pysftp.

How to verify host key with Python pysftp?

To verify host key with Python pysftp, we create a pysftp.Connection object.

For instance, we write

try:
    ftp = pysftp.Connection(host, username=user, password=password)
except:
    print("Couldn't connect to ftp")
    return False

to create a pysftp.Connection in a function to try to connect to the host with the given user and password.

If it fails, then an exception will be raised and we know the host key isn’t valid.

Conclusion

To verify host key with Python pysftp, we create a pysftp.Connection object.

Posted in Python, Python Answers