作者chaotic0307 (坚持、耐心、努力学习)
看板Python
标题[问题] 透过ssh连到资料库的问题
时间Mon May 6 13:39:08 2019
大家好,小弟目前公司需要透过ssh跳板机连到资料库,而且是用公私钥的方式
有google很多网页资料,但试了很多结果都不行,想知道问题到底出在哪,以下是程式码
-------------------------------------------------------
import pymysql
from sshtunnel import SSHTunnelForwarder
with SSHTunnelForwarder(
ssh_address_or_host = ('ssh跳板机的位址',22),
ssh_username = '跳板机使用者帐号',
ssh_pkey = '私钥的绝对路径',
remote_bind_address = ('资料库位址', 3306)
) as server:
conn = pymysql.connect(
user = '资料库使用者帐号',
password = '资料库使用者密码',
database = '资料库名称',
port = server.local_bind_port,
charset = 'utf8')
cur = conn.cursor()
sql = 'SELECT VERSION()'
cur.execute(sql)
data = cur.fetchone()
print(data)
执行後出现以下错误讯息
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-9-5452d8f5b19d> in <module>
6 ssh_username = 'ycchiang',
7 ssh_pkey = '/Users/ycchiang/.ssh/id_rsa',
----> 8 remote_bind_address = ('int-db.data-test.internal', 3306)
9 ) as server:
10 conn = pymysql.connect(
~/anaconda3/lib/python3.7/site-packages/sshtunnel.py in __init__(self,
ssh_address_or_host, ssh_config_file, ssh_host_key, ssh_password, ssh_pkey,
ssh_private_key_password, ssh_proxy, ssh_proxy_enabled, ssh_username,
local_bind_address, local_bind_addresses, logger, mute_exceptions,
remote_bind_address, remote_bind_addresses, set_keepalive, threaded,
compression, allow_agent, host_pkey_directories, *args, **kwargs)
903 allow_agent=allow_agent,
904 host_pkey_directories=host_pkey_directories,
--> 905 logger=self.logger
906 )
907
~/anaconda3/lib/python3.7/site-packages/sshtunnel.py in
_consolidate_auth(ssh_password, ssh_pkey, ssh_pkey_password, allow_agent,
host_pkey_directories, logger)
1094
1095 if not ssh_password and not ssh_loaded_pkeys:
-> 1096 raise ValueError('No password or public key available!')
1097 return (ssh_password, ssh_loaded_pkeys)
1098
ValueError: No password or public key available!
请大大们帮忙解惑~
如果是用command line的方式可以正确连线,只是想知道上面这种方式该怎麽处理
谢谢各位。
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 218.210.99.87
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1557121150.A.4FC.html
1F:推 TitanEric: 会不会是public key路径填错 05/06 18:48
2F:→ TakiDog: 先测好ssh 05/07 15:16
3F:推 s860134: 你的跳板有开 sshconfig forwarding 吗 05/08 23:17