我正在尝试使用SshTunnelForwarder编写脚本以通过ssh通过ssh连接到mysql服务器,如下所示:
import sshtunnel
import MySQLdb
def getValueFromDB(dbusername, query, ssh_pass, db_pass):
with sshtunnel.SSHTunnelForwarder(("servername", 2222),
ssh_password=ssh_pass,
ssh_username=dbusername,
remote_bind_address=("127.0.0.1", 3306)) as tunnel:
conn = MySQLdb.connect("127.0.0.1", dbusername, db_pass, port=tunnel.local_bind_port)
cursor = conn.cursor()
cursor.execute(query)
value = cursor.fetchone()[0]
conn.close()
return value
getValueFromDB("user", "show databases;", "pass1", "pass2")
但是我最终遇到错误:
type(host).__name__
AssertionError: IP is not a string (NoneType)
尝试了所有可能的方法,但无济于事。 有人可以帮我解决问题吗?
我也尝试过使用“ ssh_address_or_host”,但是没有运气。