How to port forward using python - python

I have a remote server containing a database whose address is private. When i run the script i get
server connected
Connection Failed
import psycopg2
from sshtunnel import SSHTunnelForwarder
import os
try:
with SSHTunnelForwarder(
('Remote_server_ip', Remote_server_port),
ssh_private_key="/home/User/.ssh/id_rsa",
ssh_username="xxxxx",
remote_bind_address=(Database_private_ip, Database_private_port)) as server:
server.start()
print ("server connected")
params = {
'database': 'xxxxx',
'user': 'xxxxx',
'password': 'xxxxxx',
'host': 'localhost',
'port': PORT
}
conn = psycopg2.connect(**params)
curs = conn.cursor()
print ("database connected")
except:
print ("Connection Failed")
I'm thinking port forwarding is still not being executed so that i can be able to access the db thus why i don't get database connected
How do i solve this?

Related

Python: Connect to an Azure PostgreSQL instance through SSH Tunnel

I am trying to use Python to connect to a PostgreSQL instance, which is located on Azure through an SSH tunnel. I can connect to the database with DBeaver with no Problem.
Here is the code that I am using.
from sshtunnel import SSHTunnelForwarder
server = SSHTunnelForwarder(
('160.**.**.**', 22),
ssh_username="*******",
ssh_password="*******",
remote_bind_address=('localhost', 5432))
server.start()
print("server connected")
params = {
'database': '*******',
'user': '*****postgresadmin#*****dev-postgres',
'password': '************',
'host': '**********-postgres.postgres.database.azure.com',
'port': server.local_bind_port
}
conn = psycopg2.connect(**params)
cur = conn.cursor()
text = "select * from table"
cur.execute(text)
However I get the following error:
conn = _connect(dsn, connection_factory=connection_factory, **kwasync) psycopg2.OperationalError: could not translate host name "**********-postgres.postgres.database.azure.com" to address: Unknown host
I also tried sqlalchemy using this with the same result.
Any idea on what I am doing wrong? Do I maybe need the IP-address of the host instead of the domain?
The SSHTunnelForwarder is used if you want to do some stuff on the remote server.
Another code block is needed if you need to use remote server as a bridge to connect to another server:
import sshtunnel
with sshtunnel.open_tunnel(
(REMOTE_SERVER_IP, 443),
ssh_username="",
ssh_password="*******",
remote_bind_address=(AZURE_SERVER_HOST, 22),
local_bind_address=('0.0.0.0', 10022)
) as tunnel:
params = {
'database': '*******',
'user': '*****postgresadmin#*****dev-postgres',
'password': '************',
'host': '127.0.0.1',
'port': 10022
}
conn = psycopg2.connect(**params)
cur = conn.cursor()
text = "select * from table"
cur.execute(text)

Connect to MySQL via SSH Tunnelling | MySQL Connection not available

I am using the following python snippet to connect my MySQL database on a shared hosting server.
import mysql.connector
import sshtunnel
with sshtunnel.SSHTunnelForwarder(
('server.web-hosting.com', 21098),
ssh_username = 'ssh_username',
ssh_password = 'ssh_pass!23',
remote_bind_address = ('127.0.0.1', 3306)
) as tunnel:
connection = mysql.connector.MySQLConnection(
user = 'db_user',
password = 'db_pass',
host = '127.0.0.1',
port = tunnel.local_bind_port,
database = 'demo',
)
mycursor = connection.cursor()
query = "SELECT * FROM sample_table"
mycursor.execute(query)
I am getting the following error. I am able to connect to the database using DBeaver though.
MySQL Connection not available.

Django database connection with Host and Instances with SQL Server

I'm using SQL Server as the backend for the database. In the setting.py file, I need to use the host name with instance. Due to this, I got the error given below:
The above exception (('08001', '[08001] [Microsoft][SQL Server Native Client 11.0]TCP Provider: No connection could be made because the target machine actively refused it.\r\n (10061) (SQLDriverConnect); [08001] [Microsoft][SQL Server Native Client 11.0]Login timeout expired (0); [08001] [Microsoft][SQL Server Native Client 11.0]Invalid connection string attribute (0); [08001] [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (10061)')) was the direct cause of the following exception:
# "settings.py"
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'xxxxx',
'USER': 'xxx',
'PASSWORD': 'xxxx',
'PORT': '1433',
'HOST': 'aaa\bbb',(hostname\instance)
'OPTIONS': {
'driver': 'SQL Server Native Client 11.0',
},
}
}
How could I resolve this error and connect with my database?
Your post is near two years ago, but I faced the same issue now.
By looking on https://github.com/michiya/django-pyodbc-azure/issues/201
It seems that the problem comes from the way the port is included in the connection string. The issue will be solved when the port is specified in this way:
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'xxxxx',
'USER': 'xxx',
'PASSWORD': 'xxxx',
'HOST': 'aaa\bbb',(hostname\instance)
'OPTIONS': {
'driver': 'SQL Server Native Client 11.0',
},
'extra_params': 'PORT=1433'
I tried it, it works fine.
Since your settings.py looks fine,I think its a general issue that can be caused from multiple wrong configurations on the database side, such as:
TCP/IP Connections are disabled in the SQL Server Configuration Management
In TCP/IP properties in the "IP ALL" section port 1433 could not be configured, so it refuses to your requests
One of many SQL windows services suddenly could be stopped
I found this stack overflow post very useful with multiple answers that covers each of the points above.
You can use the latest package mssql-django to connect Django to MSSQL(SQL Server) with SQL Server Authentication. *I use SQL Server 2019 Express.
So, try this code below. *"ENGINE" must be "mssql" and keep it blank for "PORT" because there will be error if setting any port number e.g. "2244", "9877" or even "1433" which is the default port number of MSSQL:
# "settings.py"
DATABASES = {
'default': {
'ENGINE': 'mssql', # Must be "mssql"
'NAME': 'xxxxx',
'USER': 'xxx',
'PASSWORD': 'xxxx',
'HOST': 'aaa\bbb',(hostname\instance)
'PORT': '', # Keep it empty
# 'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
},
},
}

Attempting to do remote login over SSH connection using python script

I am trying to log in to a remote server after setting up an SSH connection. I can get the SSH Tunnel to work, but from there I'm stumped on how to log in.
I am currently using Python's sshtunnel extension, which successfully connects me to the place I need. Now I need to be able to log in which I would usually do through a terminal with the command "ssh remotepc-ssh" (I have all the information stored in a /.ssh/config file)
I have already tried paramiko but it continuously gives me errors or won't load in the end. I also tried pysftp which is what I show in the code below but when I try to get the my known_hosts it tells me that it isn't finding any known ones, and when I try to set hosts to None, the system won't connect.
from sshtunnel import SSHTunnelForwarder
#import paramiko
import pprint
import pysftp
# create SSH tunnel
SERVER_HOST = "hostname"
SERVER_PASS = "********"
LOGIN_HOST = "loginUsername"
LOGIN_PASS = "*********"
server = SSHTunnelForwarder(
SERVER_HOST,
ssh_password = SERVER_PASS,
remote_bind_address=('127.0.0.1', 8080)
)
try:
server.start()
print("Connected to SSH Tunnel with local bind port %s" %server.local_bind_port)
###################################
# BROKEN PART BELOW
# establish remote connection
try:
print("\nEstablishing connection to %s" %LOGIN_HOST)
cnopts = pysftp.CnOpts()
cnopts.hostkeys.load = ("known_hosts")
with pysftp.Connection(LOGIN_HOST, password = LOGIN_PASS, cnopts = cnopts) as sftp:
print("CONNECTED")
except:
print("Unable to connect")
pass
##################################
server.stop()
print("\nServer stopped. Goodbye")
except:
print("Could not connect")
pass
I've just came by and solved the same issue, so let me share my working example:
with SSHTunnelForwarder(
("SSH_HOST",22),
ssh_username="SSH_USERNAME",
ssh_pkey="SSH_KEY",
remote_bind_address=("SFTP_HOST", 22),
) as tunnel:
# do some operations with client session
with pysftp.Connection(host='127.0.0.1', port=server.local_bind_port, username="SFTP_USERNAME",
private_key="SFTP_KEY", cnopts=cnopts) as sftp:
print("Connection succesfully stablished ... ")
print(sftp.listdir('public'))
print('FINISH!')

Connecting to PostgreSQL database through SSH tunneling in Python

I am trying to connect to a server remotely and then access it's local database with Python. I am successfully connecting to the server, although I can't seem to connect to the database on the server. My code is below:
import psycopg2
from sshtunnel import SSHTunnelForwarder
try:
with SSHTunnelForwarder(
('<server ip address>', 22),
ssh_private_key="</path/to/private/ssh/key>",
ssh_username="<server username>",
remote_bind_address=('localhost', 5432)) as server:
print "server connected"
conn = psycopg2.connect(database="<dbname>",port=server.local_bind_port)
curs = conn.cursor()
print "database connected
except:
print "Connection Failed"
These are pieces of code I have found on the internet and pieced together. I have also tried the connection statements below in place of the code above:
params = {
'database': '<dbname>',
'user': '<dbusername>',
'password': '<dbuserpass>',
'host': 'localhost',
'port': 5432
}
conn = psycopg2.connect(**params)
I know I can connect to the database because on my machine; I am able to use sqlectron to tunnel in and connect appropriately.
Just in case it is not clear what I am trying to do from above, I need to ssh tunnel into my remote server using a private ssh key on my computer (working properly), and then I need to connect to a PostgreSQL database that is on localhost at port 5432.
I am currently getting the current error message for both ways of trying to connect:
2016-01-23 11:16:10,978 | ERROR | Tunnel: 0.0.0.0:49386 <> localhost:5432 error: (9, 'Bad file descriptor')
I don't know if this may be helpful, but I had to connect to a PostgreSQL database through SSH tunneling as well. I succeeded to connect using your code with some modifications:
import psycopg2
from sshtunnel import SSHTunnelForwarder
try:
with SSHTunnelForwarder(
('<server ip address>', 22),
#ssh_private_key="</path/to/private/ssh/key>",
### in my case, I used a password instead of a private key
ssh_username="<server username>",
ssh_password="<mypasswd>",
remote_bind_address=('localhost', 5432)) as server:
server.start()
print "server connected"
params = {
'database': '<dbname>',
'user': '<dbusername>',
'password': '<dbuserpass>',
'host': 'localhost',
'port': server.local_bind_port
}
conn = psycopg2.connect(**params)
curs = conn.cursor()
print "database connected"
except:
print "Connection Failed"
After adding server.start(), the code worked nicely. Furthermore, inverted commas were missing after 'database connected'.
I hope this might be helpful to you, thanks for sharing your code!
Both these examples were very helpful. I just needed to combine the good parts from both.
from sshtunnel import SSHTunnelForwarder #Run pip install sshtunnel
from sqlalchemy.orm import sessionmaker #Run pip install sqlalchemy
from sqlalchemy import create_engine
with SSHTunnelForwarder(
('<remote server ip>', 22), #Remote server IP and SSH port
ssh_username = "<username>",
ssh_password = "<password>",
remote_bind_address=('<local server ip>', 5432)) as server: #PostgreSQL server IP and sever port on remote machine
server.start() #start ssh sever
print 'Server connected via SSH'
#connect to PostgreSQL
local_port = str(server.local_bind_port)
engine = create_engine('postgresql://<username>:<password>#127.0.0.1:' + local_port +'/database_name')
Session = sessionmaker(bind=engine)
session = Session()
print 'Database session created'
#test data retrieval
test = session.execute("SELECT * FROM database_table")
for row in test:
print row['id']
session.close()

Categories