Lua TCP Socket check VPN connection - sockets

I'm trying to figure out if a VPN server's port is active with Lua socket. The default port of my VPN server is 1723, but I can't tell if the port is open using a tcp socket on this port. Socket just waiting
require "socket"
local sock = socket.tcp()
local s = sock:connect("75.182.115.121", "1723")
sock:close()
if s then
ngx.print("Port is open.")
end

Related

How can I check a socket from a webserver?

Im doing a challenge (CTF style) and everyting we got is an IP.
Scanning that IP only one port is open.
If I connect to that IP and port using netcat, I got a kind of "dance" doing in CMD, with a message at the end that says "Check socket 12345".
I need to understand again what truly a socket is because im not getting anywhere trying to connect to that socket.
Its possible to connect to a socket from a specific port? or I only can make a connection from a open port and there the web servers redirect my connection automatically to a socket?
You can use netcat nc and its -p option to set the source port.
Netcat man page say:
-p port
local port number (port numbers can be individual or ranges: lo-hi [inclusive])
Try "nc -p 12345 dest_IP dest_port"

Bi-directional communication using sockets via ssh tunnel

I have a server host (S) and a bunch of clients (C1, C2, C3, ...). I would like to open connection between S and C1, C2, C3 respectively for bi-directional communication. Ideally using sockets. SSH for authorisation purposes is preferred.
Ideally:
Client C1 creates SSH reverse tunnel to S, forwarding C1's port so it is accessible on S as it's own port.
Client program running on C1 creates a socket and binds to forwarded port.
Server program running on S creates a socket and binds to forwarded port.
Client and server can exchange data.
Is something like that possible? I tried coding up a draft using Python but to no avail:
Firstly, I run on C1: ssh -N -R 9999:localhost:15432 root#example.com - OK
Secondly, on server I run:
import socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(('localhost', 9999))
serversocket.listen(5)
while True:
connection, address = serversocket.accept()
buf = connection.recv(64)
if len(buf) > 0:
print buf
break
Thirdly, on client I run:
import socket
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect(('localhost', 15432))
clientsocket.send('hello')
But I'm getting socket.error: [Errno 111] Connection refused on client. Also if I set up tunnel first and then launch server program, I'm getting [Errno 98] Address already in use. It only works when I first start the program, then set up a tunnel.
If aforementioned concept does not make sense, what would you suggest to create sort of synchronisation tool so any client listens for queries from the server and can respond with data? (preferrably in Python).
Thanks in advance.
-N doesn't do what you seem to think it does: it's intended for the ssh-destination to be able to connect back to the originator. But that would make the originating side the server.
It sounds like you should be using -L to simply create a connection from the client through the ssh tunnel to the server.
To demonstrate: I have a local server named 'bree'. On that machine, I execute (this could also be your python server listening on port 9999):
nc -l 9999
Now on my client machine, I execute this in one window (or could put it in the background):
ssh -N -L 9999:bree:9999 bree
This says: listen on the local (client) machine to port 9999, and when a connection to it is made, forward the request through the tunnel, and connect to port 9999 on bree.
Now, in a second window on the client machine, I execute:
nc localhost 9999
The two nc instances are connected.

Configure MongoDB to listen only on Unix Socket, not on any TCP port

How to configure mongo DB to not to listen on any port, but only on unix domain socket?
Since my app and DB will run on same server, the two will communicate only via Unix domain sockets. To prevent any outside access to the mongoDB server I also want to disable it from listening on any TCP port.

Python sockets will not connect

I'm trying to run a server and a client on two separate Windows 7 machines on the same network using sockets in Python 2.7. First I'm just trying to get them to connect before trying to do anything.
Currently my server is:
import socket
host = '0.0.0.0' #Also tried '', 'localhost', gethostname()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, 12345))
s.listen(5)
cs, addr = s.accept()
print "Connected."
My client is:
import socket
host = '127.0.0.1' #Also tried 'localhost', gethostname()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(host, 12345)
print "Connected."
The error I get is:
socket.error: [Errno 10061] No connection could be made because the target machine actively refused it.
I've looked through lots of other questions but none of the answers solved my problem. Any help is appreciated.
When I use the IP address of the server (10.0.63.40) as the host for the client, I get
[Errno 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
You are saying these are two separate machines. You cannot reach the one machine from the other by connecting to 127.0.0.1 or localhost.
Listening on 0.0.0.0 is fine, this means that the listening socket is reachable from all interfaces, including the local network.
However, for connecting to your server, you obviously need to use the IP address (or hostname, if you have properly set up a local name server) of your server machine in the local network.
Per your comment, the local IP address of your server machine is 10.0.63.40. That means you should end up calling s.connect("10.0.63.40", 12345).
I had the same problem when I tried to connect my client code with server one. It got resolved my by using this command:
socket.gethostbyname(socket.gethostname())
: note[ I run it locally not uploaded it into a live server]

interpret NetStat -a for a postgres remote connection

When I run netstat -a | findstr :5432 I get:
TCP 0.0.0.0:5432 PDDV-Answers:0 LISTENING
TCP 127.0.0.1:5432 PDDV-Answers:53925 ESTABLISHED
...
TCP 127.0.0.1:53931 PDDV-Answers:5432 ESTABLISHED
TCP [::]:5432 PDDV-Answers:0 LISTENING
Is the postgres DB on this server listening for remote connections on 5432?
I was expecting something like:
TCP 0.0.0.0:5432 *.*:0 LISTENING
My settings in postgres have all been enabled for remote connections and listening. and I think my firewall rule in is place - yet I can't remote telnet to the server on 5432 (local telnet to it works), or establish a database connection from my remote server which is my ultimate objective.
Craig Ringer was correct. The system is listening fine. There was a corporate firewall which was blocking access into the server.
In addition I needed to add additional access rules via the pg_hba.conf file for the servers own IP number
host all all a.b.c.d/32 md5
To allow it to connect to itself via Telnet. That was how I proved it was a corporate firewall problem