Port Forwarding in python - python

I am writing some python code to query a remote sql database. I can do this in a command line by setting up port forwarding and, in a different shell, running a mysql server. However I cant think of a way of doing this in python - is there a python module which would set up port forwarding, then return control back to the python script (and even better if I can periodically check to see if the port forwarding is still active!) TIA, Jack

Related

How to use WHOIS queries from python script when port 43 is likely not accessible

Im very new to network programming and faced a following problem:
Im working on VMware CentOS7 virtual machine on Windows10 host.
My script should send WHOIS queries and parse their output (e.g. expiration date).
However, an attempt to send a query leads to a connection error:
>>>import whois
>>>whois.query('google.com')
WhoisCommandFailed: connect: Network is unreachable
I tried to whois from terminal, but error was the same.
When i tried to use whois directly from Windows, which hosts virtual machine, the error seemed to look same as well (connection timeout).
As i found out, it was most likely related to access to port 43. I created rules (for in and out) for Windows firewall for this port by a guide , but error still persisted.
It looks like access to this port was blocked by ISP (however ping command is working).
To sum up, I got two questions there:
1) (less important) How to check if port 43 is blocked by firewall either by ISP?
2) (most important) Is it possible somehow to reconfigure WHOIS for usage of another port (i.e. 23) for sending queries by Python script?
Unfortunately, ISP security policy doesn't allow them to open 43 port.
Mostly ISP doesn't block any port but yes, this is not 100% true.
Testing connection:
run tcpdump (install command: yum install tcpdump) command on CentOS: tcpdump -peni any tcp and port 43
You have to see lines with the following text: 192.168.1.1.57350 > 192.34.234.30.43 where 192.34.234.30 IP address means the remote whois server.
Try to telnet to remote server's TCP/43 port: telnet 192.34.234.30 43
You should see the following:
Trying 192.34.234.30...
Connected to 192.34.234.30.
Escape character is '^]'.
If you can`t see context like that and you get back prompt immediately you will a firewall rule somewhere what is block connection. I recommend to switch off firewall temporarily and test again.
You cannot change port number, because it is configured on the remote side, on the server.
Can CentOS7 server communicate towards the internet? In example can you install packages?
Is there any router between windows machine and ISP?

find available port to run server

What's the best approach in python to do something like what happens when you run jupyter notebook, in other words, run a server (for example, with http.server) on some available port if the default one isn't available? Is it common to just catch the error if starting the server fails and try a different port until it works?
You can use port 0 - this will bind your server to some port that is currently known to be available by kernel. However, that makes a problem of service discovery - how your clients will know which port number server is listening on? If that's only you, shouldn't be big deal.

What do I use for HOST to connect to a remote server with mysqldb python?

Things to note in advance:
I am using wampserver 2.2
Ive forwarded port 80
I added a rule to my firewall to accept traffic through port 3306
I have added "Allow from all" in directory of "A file i forget"
My friend can access my phpmyadmin server through his browser
I am quite the novice, so bear with me.
I am trying to get my friend to be able to alter my databases on my phpmyadmin server through
python. I am able to do so on the host machine using "127.0.0.1" as the HOST. My Question is, does he have to use my external ip as the HOST or my external ip/phpmyadmin/ as the HOST? And if using the external ip iscorrect...What could the problem be?
If your phpmyadmin runs on the same machine as mysql-server, 127.0.0.1 is enough (and safer if your mysql server binds to 127.0.0.1, rather than 0.0.0.0) if you use tcp(rather than unix socket).

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.

Start a script on a remote machine through ssh with python - but in reverse?

Here's what I need to do:
The user is on a remote machine and connects to a server via ssh. He runs a python script on the server. The script running on the server starts a script on the user's remote machine as a subprocess and opens a pipe to it for communication.
First, is this at all possible?
Second, is this possible in such a way that the user does not need to do anything fancy, like open up a reverse ssh tunnel? If they do have to open up a reverse ssh tunnel, can I figure out which port they are using?
First, is this at all possible?
With simple user --SSH--> server - no, its not.
is this possible in such a way that the user does not need to do anything fancy
User will have to run sshd on his machine, add a user for your server and somehow let you to connect to it, bypassing NAT if any. So no, there is no such way with SSH.
I'd go with client-side application. Give script to user and let him run the script instead of SSH'ing. If you need to communicate with server, you can use something like paramiko on client side to connect to server from script. Then, script can launch other applications on client's computer based on data it receives from server.

Categories

Resources