I have a paid account with PythonAnywhere, with MySQL DB. I am able to connect to DB using DBeaver and am also able to SSH using command prompt (ssh[email protected]).
However when I use the same credentials to run the code in Jupyter Notebook in my local, I get the error:
Password is required for key C:\Users{myComputerUserName}/.ssh\id_rsa.... Could not connect to gateway ssh.pythonanywhere.com:22 :Incompatible ssh peer (no acceptable host key)
This is the notebook code I am using:
sshtunnel.SSH_TIMEOUT = 10.0sshtunnel.TUNNEL_TIMEOUT = 10.0with sshtunnel.SSHTunnelForwarder( ('ssh.pythonanywhere.com'), ssh_username='MyUserName', ssh_password='MyPWD', remote_bind_address=('MyUserName.mysql.pythonanywhere-services.com', 3306)) as tunnel: connection = MySQLdb.connect( user='MyUserName', passwd='MyPWD', host='127.0.0.1', port=tunnel.local_bind_port, db='MyUserName$DBName', ) q1 = f"""SELECT session_key, session_data, expire_date FROM django_session;""" cursor = connection.cursor() cursor.execute(q1) print(cursor.fetchall()) connection.close()I don't know much aboutSSH and am not able to figure out the issue. Have read all the PA and StackOverflow forums, but can't find an answer. Can someone point me in the right direction?
1 Answer1
Try using key-based auth inSSHTunnelForwarder (notssh_password):
import sshtunnelimport MySQLdb...with sshtunnel.SSHTunnelForwarder( ('ssh.pythonanywhere.com', 22), # assuming your account is on www.pythonanywhere.com ssh_username='MyUserName', ssh_pkey=r'C:\Users\myComputerUserName}\.ssh\id_rsa', # ssh_private_key_password='your_key_passphrase', # only if your key has passphrase remote_bind_address=('MyUserName.mysql.pythonanywhere-services.com', 3306),) as tunnel: conn = MySQLdb.connect( host='127.0.0.1', port=tunnel.local_bind_port, user='MyUserName', passwd='MyPWD', db='MyUserName$DBName', ) with conn as c: ...If you still see “no acceptable host key”, upgrade your client libs so they support modern host keys/algorithms:
pip install -U paramiko cryptography sshtunnelComments
Explore related questions
See similar questions with these tags.
