0

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 say"

"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. "

And give an example using PuTTY

"Create a session in PuTTY using your server IP-address as hostname and port 21098"

The point is that I need my Python script to do this automatically without any prompting for password, etc.

Have read something about paramiko but didn't get the point as SSH is something new to me (apart of accessing my linux machine).

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 [email protected] 

Edit:
Great, something is at least happening now after having cleaned up my python directory (remaining paramiko.py file created problems). Also made a small change on line 2 of your script (ssh = SSHClient() ->> ssh = paramiko.SSHClient())

Then did the following:

ssh -p 21098 [email protected]

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

/home/daniel/python_scripts/sshkey

back to my local machine I then run below python script

#!/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....

Whatsoever, there is something that I did not understand and obviously did wrong.....

1 Answer 1

2

Paramiko is really what you're looking for.

Basically, it is an SSH Client (like PuTTY, for one..) that even has TTY support. Essentially, you would use their class SSHClient and call the connect method. You can do it without a password. However, you will need a public key, which paramiko also supports in lieu of a password.

So, somewhere along the line, when you do ssh -p 21098 [email protected], what you're saying is to server137, check the public key in my hostkeys file, and please verify I can connect.

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 documentation for SSHClient here: Paramiko Client docs

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.