简体   繁体   English

Python脚本通过SSH隧道连接到Namecheap上的MySQL数据库

[英]Python script to connect via SSH tunnel to MySQL database at Namecheap

I am trying something since two days googling and reading forums without any success. 自从过去两天谷歌搜索和阅读论坛以来,我一直在尝试某些尝试,但没有成功。

I have a MySQL database hosted at Namecheap.com that I need to access from my local linux machine via a Python script for creating tables and entries. 我在Namecheap.com上托管有一个MySQL数据库,我需要通过Python脚本从本地linux计算机访问该数据库以创建表和条目。 Namecheap say" Namecheap说“

"Remote MySQL connection is disabled on our shared servers due to security reasons, but you can easily setup SSH tunnel between your PC and our server using SSH-client (for example, Putty) with the MySQL port (3306) forwarding. After completing it you will have port 3306 on your local machine listening and forwarding to your remote server's localhost on port 3306. Thus you can connect to the remote server's MySQL database effectively as though it were running on your local box. " “出于安全原因,我们共享服务器上的远程MySQL连接被禁用,但是您可以使用SSH客户端(例如Putty)通过MySQL端口(3306)转发,轻松地在PC和我们的服务器之间设置SSH隧道。完成后您将在本地计算机上拥有端口3306,以侦听并转发到端口3306上的远程服务器的本地主机。因此,您可以有效地连接到远程服务器的MySQL数据库,就像它在本地计算机上运行一样。”

And give an example using PuTTY 并举例说明使用PuTTY

"Create a session in PuTTY using your server IP-address as hostname and port 21098" “使用服务器的IP地址作为主机名和端口21098在PuTTY中创建会话”

The point is that I need my Python script to do this automatically without any prompting for password, etc. 关键是我需要我的Python脚本自动执行此操作,而无需任何提示输入密码的提示,等等。

Have read something about paramiko but didn't get the point as SSH is something new to me (apart of accessing my linux machine). 已经阅读了有关paramiko的一些知识,但并没有理解重点,因为SSH对我来说是新事物(访问我的Linux机器除外)。

I can successfully login manually via command line into my hosting account after having entered password, but this is just about all because then do not know how to run then script that is on my machine. 输入密码后,我可以通过命令行成功手动登录到我的托管帐户,但这仅是全部操作,因为那时不知道如何运行计算机上的脚本。

ssh -p 21098 my_user_name@server137.web-hosting.com 

Edit: 编辑:
Great, something is at least happening now after having cleaned up my python directory (remaining paramiko.py file created problems). 太好了,在清理完我的python目录之后,至少现在正在发生某些事情(剩下的paramiko.py文件创建了问题)。 Also made a small change on line 2 of your script (ssh = SSHClient() ->> ssh = paramiko.SSHClient()) 还对脚本的第2行进行了小的更改(ssh = SSHClient()->> ssh = paramiko.SSHClient())

Then did the following: 然后执行以下操作:

ssh -p 21098 my_username@server137.web-hosting.com

to login into the remote host, and after successful login entering my password 登录到远程主机,并在成功登录后输入我的密码

ssh-keygen -t rsa

created a key without pasphrase which I afterwards recuperated via ftp to save it in my local machine folder 创建了一个没有密码的密钥,之后我通过ftp恢复了该密钥以将其保存在本地计算机文件夹中

/home/daniel/python_scripts/sshkey

back to my local machine I then run below python script 回到我的本地机器,然后在python脚本下运行

#!/usr/bin/python
import paramiko

#clean the screen
os.system('clear')
myPkey = paramiko.RSAKey.from_private_key_file('/home/daniel/python_scripts/sshkey/key')
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #if you want to auto add the host RSA key
ssh.connect('server137.web-hosting.com', 21098, 'my_username', pkey=myPkey)

sys.exit()

but this is what I got: 但这就是我得到的:

Traceback (most recent call last):
File "./my_vimeo.py", line 13, in <module>
ssh.connect('server137.web-hosting.com', 21098, 'my_username', pkey=myPkey)
File "/usr/local/lib/python2.7/dist-packages/paramiko/client.py", line 307, in connect
look_for_keys, gss_auth, gss_kex, gss_deleg_creds, gss_host)
File "/usr/local/lib/python2.7/dist-packages/paramiko/client.py", line 519, in _auth
raise saved_exception
paramiko.ssh_exception.AuthenticationException: Authentication failed.

'my_username' is obviously not the one as shown.... “ my_username”显然不是如图所示的那个。

Whatsoever, there is something that I did not understand and obviously did wrong..... 无论如何,有些事情我不理解,显然做错了.....

Paramiko is really what you're looking for. Paramiko确实是您想要的。

Basically, it is an SSH Client (like PuTTY, for one..) that even has TTY support. 基本上,它是一个甚至具有TTY支持的SSH客户端(例如PuTTY。)。 Essentially, you would use their class SSHClient and call the connect method. 本质上,您将使用其类SSHClient并调用connect方法。 You can do it without a password. 您无需密码即可完成此操作。 However, you will need a public key, which paramiko also supports in lieu of a password. 但是,您将需要一个公共密钥,paramiko也支持该公共密钥来代替密码。

So, somewhere along the line, when you do ssh -p 21098 my_user_name@server137.web-hosting.com , what you're saying is to server137, check the public key in my hostkeys file, and please verify I can connect. 因此,在某处,当您执行ssh -p 21098 my_user_name@server137.web-hosting.com ,您的意思是对server137进行检查,请检查我的hostkeys文件中的公钥,并请确认我可以连接。

You could then use the public key instead: 然后,您可以改用公共密钥:

import paramiko

myPkey = paramiko.RSAKey.from_private_key_file('<private_key_path_on_your_server>')
ssh = SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #if you want to auto add the host RSA key
ssh.connect('server137.web-hosting.com', 21098, 'my_user_name', pkey=myPkey

You can see how to set up your keys here: Paramiko ssh connection without password 您可以在此处查看如何设置密钥: 无密码的Paramiko ssh连接

Paramiko documentation for SSHClient here: Paramiko Client docs 此处的SSHClient的Paramiko文档:Paramiko Client docs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM