Connect to localhost mongodb from python script started by torify - python

When I'm trying to connect to the mongodb from my python script I see this warning many times and error finally:
[Dec 13 11:58:56] WARNING torsocks[8133]: [connect] Connection to a local address are denied since it might be a TCP DNS query to a local DNS server. Rejecting it for safety reasons. (in tsocks_connect() at connect.c:177)
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 1] Operation not permitted
I use torify command for starting my command - torify python myscript.py. Without torify it works.
What I'm doing wrong? The same situation is on another machines to.

I found a solution for this case. I have to create a Hidden Service first. To do that I have to add this lines to my tor config file (/etc/tor/torrc):
HiddenServiceDir /tmp/tormongo
HiddenServicePort 27017 127.0.0.1:27017
After that I need to restart my tor service. If everything was done right the folder has to appear in /tmp. In this folder will be the new file hostname with the string like sgwqrpepus3lwcke.onion. This is the host what I can use instead of localhost in mongodb connection settings.
Or I can add a new ENV variable for this setting and set it every script start by using command like that:
export DB_HOST=$(cat /tmp/tormongo/hostname) && torify python /var/www/myproject/myscript.py

Related

Ubuntu MYSQL cannot connect from python script

I need to create a mysql DB for my project. I have a ubuntu server and have followed this guide to install required modules:
https://www.javahelps.com/2018/10/install-mysql-with-phpmyadmin-on-ubuntu.html
After installing everything I can reach the phpmyadmin using the browser 192.168.3.146/phpmyadmin works without any issues:
phpmyadmin home screen
But the problem is when I try to reach the database through the python code, it does not work. My program hangs after mysql.connector.connect and will never print the next print statement.
print("trying MYSQL")
myConnection = mysql.connector.connect(host=192.168.3.146, user='test_user', passwd='test_user123', db='test', autocommit=True)
print("Connection ID:", myConnection)
print(myConnection)
I have previously sucesfully connected to the mysql this way to the mysql server which was created on Raspbian and did not have any issues. It seems that UBUNTU is not as straightforward.
Please can someone suggest me what could be the problem if I can easily reach the database through the web browser but python program fails
UPDATE
Trying out to comment out bind address in the configuration file but still no luck. The configuration file:
enter image description here
I have allowed firewall through port 3306 with the following command:
sudo ufw allow 3306
There is some progress. Now my Python program does not hang on the mysql connection but instead gives me error:
ERROR 1130 (HY000): Host '192.168.3.251' is not allowed to connect to this MySQL server
First of all, I am unsure where does this IP come from 192.168.3.251. The machine am using to connect to the database is configured with static IP and it is 192.168.4.200. I do not know why it says 192.168.3.251
Is the python script running from another machine? If so, maybe the problem is that mysql is listening only in localhost interface. You can change this by editing the file mysqld.cnf located in the /etc/mysql/mysql.conf.d/ folder and comment (with an #) the line that says:
bind-address = 127.0.0.1.
Once you've done this, restart the service this way:
$ sudo systemctl restart mysql.service
And, verify that everything is ok with:
$ systemctl status mysql.service

JumpSSh in Python get_cmd_output

I am trying to connect to a remote server from a jump server. It connected to the remote server perfectly, but when I try to run a python script on the remote server, it says no directory found. Please help
gateway_session = SSHSession('host',
'unman', password='password').open()
# from jump server, establish connection with a remote server
remote_session = gateway_session.get_remote_session('host',
'username',password='password')
print(gateway_session.get_cmd_output('python /Folder/test.py'))
Try first to check where you are:
print(gateway_session.get_cmd_output('pwd;ls -alrth; ls -alrth /'))
That way, you know if there is indeed a Folder at /
The OP adds in the comments:
all I needed was to was separate the commands with a semi colon and run them using the same get output command.
The OP adds:
I just sshpass to a jump server from their, I sshpass to the remote on the same command in a shell script.

apache-airflow: channel 3: open failed: connect failed: Connection refused

I am trying to build a data processing pipline. In a nutshell the pipline includes the follwing tasks: connect to a remote SQL db, extract data, process it and output files. I've built all these steps in python. Since the db is on a server I can only run the pipline only from a private server (with a static IP address) - for security reasons. So I ran all my py scripts from my own server (ubuntu). Now, to stitch the components of the pipilne I want to use apache-airflow. I followed a couple of instructions from different sources and failed to make it work. Everything seems to run but when I run airflow webserver the local host refuses the connection (for some reason). My question is rather general. I want to know if there is a good step by step instruction on how to install and use apache airflow specifically on remote server (ubuntu).
I have tried the following:
https://medium.com/#taufiq_ibrahim/apache-airflow-installation-on-ubuntu-ddc087482c14
https://vujade.co/install-apache-airflow-ubuntu-18-04/
https://towardsdatascience.com/getting-started-with-apache-airflow-df1aa77d7b1b
plus the same with docker.
Here's the log of the error I get: (connection refused)
Here's the the screen shot of when I run airflow webserver:

Python Fabric: Skip refused SSH connections?

I'm new to Python and Fabric, and I've modified a script that pings hosts on our LAN (to determine what machines are alive, we have a lot) to log into the hosts and list running processes back to the client. Whilst this works on servers, it seems there's other devices in the subnets that don't permit SSH logins and the connection is refused, causing Fabric to exit with a fatal error. Is there any way to make Fabric skip any host that refuses a connection?
Using
with settings(warn_only=True)
doesn't seem to help.
Thanks.
You can set this env var or also use this flag. Searching the docs, if you can't find it in a heading, is best.

Fabric says 'no route to host', even though I can access it over SSH

I'm having some issues uploading a file to a server with Fabric. I get the following output:
Fatal error: Low level socket error connecting to host ssh.example.com: No route to host
Aborting.
The weird thing is, when I connect manually using ssh (same host string, I copy-pasted it from the fabfile to make sure), it works perfectly as expected. I can also use scp to copy the file to the same location manually.
The offending line in my Fabfile is this, if it helps:
put('media.tgz','/home/private/media.tgz')
Also, I'm connecting to a different host to the rest of my fabfile using the #hosts() decorator (this particular method uploads static media, which is served from somewhere different to the app itself).
I had the same issue. Had not investigated it but using the IP-Address instead the hostname helped. This particular host had an IPv6 AAAA record but my client had no IPv6 connection, maybe this is the reason. HTH

Categories

Resources