When stopping SSHTunnel server the application hangs - python

I have very simple script that establishes connection to another host and then creates the tunnel through which another application connects to SQL Server.
self.dicSession['server'] = sshtunnel.SSHTunnelForwarder(
('remote_host', 22),
ssh_username="username",
ssh_password="password",
ssh_private_key="key_rsa",
remote_bind_address=('remote_host', 53425),
local_bind_address=('localhost', 1433),
)
self.dicSession['server'].start()
It works fine. However the command to stop the server. When the connection is established I am using tunnel (between 53425 port of remote host and 1433 port of localhost) to connect to a SQL Server that operate on the remote host. Up to this point everything works fine. It seems however that stopping the SSH connection with command
self.dicSession['server'].stop()
does not work properly. Despite the command to stop the connection I am still able to execute SQL statement and get results. Moreover when I execute the SQL query after the command
self.dicSession['server'].stop()
the app - which us build with usage of PyQT5 - freezes. To be honest I have not tried to run the script without GUI but I will need the GUI as the app is not only for me but for other people who are not familiar with CLI.

This happens because your client is still open.
It will allow you to stop server, once you closed your client.

Related

Postgres database refusing connection from Airflow: Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections port 5432?

I have an existing database (Postgres) that i want to connect to apache-Airflow on my host machine(Windows 10), I installed the apache-airflow on the WSL running ubuntu. The installation was smooth and working fine since i was able to get the airflow webserver running on my localhost(port:8081).
I tried connecting airflow to my existing database (carPrices) passing all the necessary parameters which were all correct. I also confirmed my database is up and running on port(5432). Whenever i click the connect button it will report this error..."could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?"
I dont know what exactly is the problem as i am new to airflow.
I tried setting the connection parameter of airflow by setting it through the airflow.cfg file and through the Airflow UI home. In the first case i cant even "airflow db init" as it report the same problem of connection refusal. the second case will setup a default sqlite db for the airflow UI to run. then i tried connecting using the UI but same error message was given.
I check using if the postgres is up and running using netstat -ab and posgres is up and listening.
I was expecting the connection to report succesful since i am sure of all the database parameters passed but instead i got this.
Found out the problem is with WSL 2, you cant connect to localhost from WSL2 without some complicated tweaks... The simplest thing to do is downgrade to WSL 1
running this command in powershell:
wsl.exe --set-version Ubuntu-20.04 1

Ngrok as portforwarding in python socket not connecting

I have a problem where the socket client wont connect to the server in python when using ngrok. Only if i run the server before starting the client it works. Also the client wont detect when server looses connection anymore. I think the client detects ngrok as a server without the python server running

Python rpyc run in background

I am using rpyc to create a Service (server). I was able to do this. Now I'm I would like to run it and connect to it using my client.
I run the server
python3 myserver.py
This runs fine. Then I run the client
python3 myclient.py
The problem is I get an error:
ConnectionRefusedError: [Errno 61] Connection refused
When I run myserver.py from Anaconda Spyder, them my client works fine, but when I run my server from the shell or command prompt, it says connection refused. Is there a specific way I need to run a rpyc server in order to connect to it?
First, check your firewall - is it open for RPyC server port
Also make sure your server uses for binding address 0.0.0.0 and not localhost (127.0.0.1)
Very important to ensure that both server and client use the same Python and RPyC versions

How to connect to ssh server from another server

I've been trying to connect to first an out-of-band management server (in my case, since I'm connecting to DELL-servers, an iDRAC) and through that connect to the main server itself. I've got it to work when I do it manually, using, in the (windows) terminal:
putty.exe -ssh 'username'#'iDRAC-IP'
followed by PuTTY window opening where I type in the password, followed by
connect
which connects to the server itself, and then I type in the username and password for the server, completing the process.
When I've been writing my script in python, I'm using paramiko, http://www.paramiko.org/, suggested here on stackoverflow, and following this example: https://www.ivankrizsan.se/2016/04/24/execute-shell-commands-over-ssh-using-python-and-paramiko/, and it works just splendid for the iDRAC (the first server I connect to). It also works when I type in
stdin, stdout, stderr = ssh_client.exec_command('connect')
because I am still in my first server (ssh_client) (I can tell this is working because when I try to connect to the server manually afterwards, it is occupied). But after that it stops working, since when doing 'connect' I am no longer in ssh_client, but in a different server.
So my question is - how do I connect to a server from another server (in this case being the out-of-band management server) and log in to this one?
You can use ssh tunnel to do so.
this post may resolve your problem:
PyCharm: Configuring multi-hop remote Interpreters via SSH

Python connection to server and interact with database

I have question how can I connect to VPS server and interact with MySQL database using python on my local machine.
I would like to make program using PyQT that connect to my server and update, take sth from db.
I know that there is MySQLdb module but from what I know i cannot connect to VPS server, because in connect method it doesn't take server password argument.

Categories