I need to SSH into a db using Python, I'm using PythonDB for this. I saw this question which details how to do this but I can't seem to get the syntax right. Would someone be able to point me in the right direction....? I also need to use a private key, how would I go about inserting that...?
ssh -L 9990:127.0.0.0:3396 <79.xxx.xx.xxx>
database = MySQLdb.connect(host'127.0.0.0', port=3306, user='jack', passwd='pass', db='test')
As said in the answer to the other question in the ssh-tunnel you are forwarding from port 9990 on your local machine to the (standard mysql) port on the remove machine. To send requests through that ssh-tunnel you need to connect to port 9990 instead of 3306:
database = MySQLdb.connect(host'127.0.0.0', port=9990, ....
Related
I am attempting to connect to a remote db2 instance. I seem to be having connection port issues or protocol issues. Below is a sample connection setting. What is the default connection port using TCPIP & python? I am reaching the server but unable to create a connection to database. Database exists.
connection = ibm_db.connect("DATABASE=DATABASE_NAME;HOSTNAME=host;PORT=50000;PROTOCOL=TCPIP;UID=username;PWD=password;", "", "")
Im receiving the following error:
Exception: [IBM][CLI Driver] SQL30061N The database alias or database name "DATABASE_NAME " was not found at the remote node. SQLSTATE=08004 SQLCODE=-30061
The error message seems clear but the cause might vary. Most likely either the database-name or the port-number is incorrect.
You get that message if a Db2-server responded indicating Db2 cannot find the specified database on HOSTNAME in the Db2-instance listening on the specified port-number.
A Db2-LUW hostname might have more than one Db2-instance running concurrently (each listening on different port-numbers), according to the hardware-resources available.
A Db2-Linux/Unix/Windows instance can have many physical databases inside it, each with a distinct name and one or more aliases.
Ask your DBA or a colleague for the correct database-name and port-number per hostname.
Alternatively ssh (or remote-desktop) to that hostname, find the owner (userid) of the process listening on port 50,000 (or whatever port you are using), become that userid (for Linux/Unix: use su or sudo ) and use db2 list db directory command to show local databases in that Db2-instance. For Db2-servers on Windows: start > db2cwadmin.bat > db2 list db directory . On Linux/Unix, use ps -ef | grep db2sysc to see how many Db2-instances are running and you can use that information (along with netstat) to discover the port on which they are listening.
I recently have been programming a program that uploads data from my Raspberry Pi weather station, with Python, to a PHPMyAdmin database. My website will then read the data in the database with PHP. I have the PHP section of the code working (will read data from the database), however my issue is my Python part of the program. I am using the MySQLdb plugin for Python. However, when I attempt to connect with the same details as PHP, it does seem to establish as a connection, as there is no error, however the rest of the code does not run.
How would I go about fixing this?
Here is my code:
import MySQLdb
db = MySQLdb.connect(
host = 'server169.web-hosting.com',
user = '***********',
passwd = '**************',
db = '************',
port = 3306 # should be same as in /etc/mysql/my.cnf on server.
)
cursor = db.cursor()
cursor.execute('SELECT VERSION()')
data = cursor.fetchone()
print str(data)
database.close()
host = 'server169.web-hosting.com',
This is a real host I think ? By default, MySQL (the DB you use) is disable on anything other than a localhost incoming connexion : you can't connect to the db from "outside" the server.
You PHP script works because it is run on the server so it can connect locally to the database.
Three ways to solve this :
connecting to you server with a SSH tunnelling raspberrypi:3306 to server169.web-hosting.com
allowing mysql to listen on server169.web-hosting.com:3306 (you'll need root access to do this, +change the firewall rules)
using an interface on the server that'll add the data for you (like a php script you call from you RaspberryPi with an HTTP POST request and a JSON object).
The last one is probably the best way for you, as you don't have to go too deep in configuration / need specials access on the server.
I have trouble connecting to my mySQL database remotely through Python.
I use the following to connect to mySQL:
import mysql.connector
cnx = mysql.connector.connect(host='XXX.XXX.XXX.X',user='XXXX',password='XXXXXX',database='testdb')
But I get the following error:
2003: Can't connect to MySQL server on '%HOST%:3306' (10060 A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond)
The server is running and when I run the same code on the computer I run the server from using 'localhost'
import mysql.connector
cnx =
mysql.connector.connect(host='localhost',user='XXXX',password='XXXXXX',database='testdb')
it works and I can modify the data in the database. I'm trying to connect it remotely from another computer though.
I've tried using GRANT ALL ON *.* TO User#Host IDENTIFIED BY 'password'; but no result. I checked my firewall and allowed all incoming and outgoing connections through port 3306 which is used by default.
I'm new to mySQL and really have no clue what to do. I don't even know if I use the correct hostname :') I use the IP address of the computer I run the server from,I think that's right.
You dont need to GRANT ALL privilage to the user. You need to tell MYSQL that this user is allowed to login from a remote location.
In fact as you are allowing remote access through this user account now, you should make sure that it can access only the database(s) it needs to, and definitely cannot use GRANT
For example
CREATE USER 'myuser'#'%' IDENTIFIED BY 'mypass';
Will allow this user to connect from any ip address. To be more secure you should try to be more specific and specify a individual ip address if you can
CREATE USER 'myuser'#'11.22.33.44' IDENTIFIED BY 'mypass';
Remember, you are creating a new user account here because you already have a
myuser#localhost
Either way you need to make sure that the password is a strong one, specially is you use the % any ip option
I have a python client that needs to talk to a remote server I manage. They communicate using zeromq. When I tested the client/server locally everything worked. But now I have the client and server deployed on the cloud, each using a different provider. My question is, what's the simplest way (that is safe) to make the connection? I'm assuming I can't pass the password over, and even if I could I'm guessing there are safer alternatives.
I know how to set an ssh connection without a password using ssh-keygen. Would that work? Would the client need to make an ssh connection with the server before sending the tcp req? If there's a python library that helps with this it'd be a big help.
Thanks!
Update:
So more than 24 hours passed and no one replied/answered. I think I'm getting closer to solve this, but not quite there yet. I added my client's key to .ssh/authorized_key on the server, and now I can ssh from the client to the server without a password. Next, I followed this post about "Tunneling PyZMQ Connections with SSH". Here's what I have in my client code:
1 context = zmq.Context()
2 socket = context.socket(zmq.REQ)
3 socket.connect("tcp://localhost:5555")
4 ssh.tunnel_connection(socket, "tcp://locahost:5555", "myuser#remote-server-ip:5555")
5 socket.send_string(some_string)
6 reply = socket.recv()
This doesn't work. I don't really understand lines 3 & 4 and I assume I do something wrong there. Also, my server (hosted on linode) has a "Default Gateway" IP and a "Public IP" -- in the tunnel connection I only specify the public ip, which is also the ip I use to ssh to the machine.
Indeed, ZMQ way is - tunnelling connection with the SSH. Your example is exactly what needs to be done, except that one should either use connect or tunnel_connection, not both.
Also, when specifying server to connect to, make sure to define the SSH port, not the ZMQ REP socket port. That is, instead of myuser#remote-server-ip:5555 you might try myuser#remote-server-ip or myuser#remote-server-ip:22.
import zmq
import zmq.ssh
context = zmq.Context()
socket = context.socket(zmq.REQ)
zmq.ssh.tunnel_connection(socket, "tcp://locahost:5555", "myuser#remote-server-ip")
socket.send(b"Hello")
reply = socket.recv()
Finally, make sure you've installed either pexpect or paramiko - they will do the tunnelling actually. Note that if you're using Windows, paramiko is the only solution which will work - pexpect openssh tunnelling won't work on Windows.
If you use paramiko instead of pexpect, make sure to set paramiko=True in the tunnel_connection arguments.
I have found ssh in Python to be iffy at best, even with paramiko and fabric libraries, so to debug, you might try setting up a tunnel separately, just to see if that's the issue with the broken connection.
For example:
ssh myuser#remote-server-ip -L 5050:localhost:5555 -N
This says: connect to myuser#remote-server-ip, and whenever I request a connection to localhost:5050 on my machine, forward it across the ssh connection so that the server at remote-server-ip thinks it's receiving a connection from localhost:5555.
-L constructs the tunnel, and -N means don't do anything else on the connection.
With that running in another shell, e.g., a different Terminal window, on your local development machine, try to connect to a zeromq server at localhost:5050, which will actually be the zeromq running on the remote server.
You could use 5555:localhost:5555 in the ssh command above, but I find that can be confusing and often conflicts with a local copy of the same service.
I am using ncclient to connect to the netconf. However when ever i try to connect through python
"ncclient.transport.errors.SessionCloseError: Unexpected session close" error is thrown. the code snippet that i am using is given below
manager.connect('<servername>',22,username='<username>')
Any help on this is much appriciated. I am able to connect to the remote server by using public key, hence i didnt provide passwordk in connect
And in the netconf server logs i am able to see access-denied error. (I got the same prob even when i tried with username and pwd)
You haven't given a lot of information.
Which version of ncclient are you using?
Which version of Python are you using?
Which NETCONF implementation are you trying to connect to? Is this to an actual switch or router, or something like a Linux server running libnetconf or yuma?
Based on the info here, I could imagine a couple of things being wrong:
paramiko isn't using the right key to establish SSH transport.
You're attempting to establish a NETCONF session with an SSH server rather than a NETCONF server.
In your script, create some logs with something like manager.logging.basicConfig(filename='ncclient.log', level=manager.logging.DEBUG) and then re-run your script - do you get anything more informative?
This is an old question, but I hope I can point you in the right direction at least.
its possible that your machines don't know each other (like when you connect via normal ssh and get the "unknown key, really connect (y/n)?" error. In that case, by default the session will not connect. To change this behavior use the "unknown_host_cb" parameter:
def allowUnknownHosts(host,fingerprint):
return True
self.manager = manager.connect(host=host, port=port, username=user,password=password, unknown_host_cb=allowUnknownHosts)