Low level TCP port openness check

B

Bedo Sandor

Hi,

I would love to create a method, that's for synchronization between
parallel programs communicating on the network (with TCP connection).
The method I need has a very simple interface and internal structure:

def wait_for_tcp_port_to_open(host, port)
loop do
#1# Send out here a TCP SYN packet to host:port
sleep 1
if #2# SYN/ACK is received
#3# Send out here an TCP RST packet to host:port
return
end
end
end # wait_for_tcp_port_to_open()

In human words: it has to wait until the host:port answers a SYN packet
with a SYN/ACK. (In any other cases it waits...) Then reset the
connection, and allow the caller method to continue.

The problem is that I don't know how to implement the above lines with
#1#, #2# and #3#. Could someone help me out with a code snippet or an
example application?


More information about the problem and the environment:

It runs on a linux box.

The method will be one puzzle of a bigger system, that has to do a 3
steps sequence:

1.) start up a server application
You can imagine this step just like starting an apache with it's init
script (locally or remotely via ssh), or sending out a Wake-on-LAN
packet to an other PC to start up, or doing anything (really anything)
that results something opening a tcp port in a few seconds or minutes
(or maybe hours).

2.) wait for the server application to really start up and listen on
the net
This would be the magic. Waiting for the tcp port to open, but don't
make a really connection to it while checking. A syn scan would be the
best, because the server application may be configured accepting only
one connection and then exiting. With a syn scan the application would
not disturbet with the checking procedure running here.

3.) start up a client for the application
This is the part where the real client is starting, that connects to
the already opened tcp port at host:port...


It's a perfect solution to lauch a linux program, that makes this if You
know one. (I don't know such one right now.)

def wait_for_tcp_port_to_open(host, port)
system "/usr/bin/xxxxxxx ... --host '#{host}' --port '#{port}' ..."
end

THX for Your help,
s.
 
A

Albert Schlef

Bedo said:
It's a perfect solution to lauch a linux program, that makes this if You
know one. (I don't know such one right now.)

Check out nmap and netstat.
 
B

Bedo Sandor

Albert said:
Check out nmap and netstat.


This is what You're suggesting, am I right?

def wait_for_tcp_port_to_open(host, port)
loop do
scan_result = `/usr/bin/nmap -n -sS '#{host}' -p
'#{port}'`
return if scan_result =~ /[\s\t]open[\s\t]/
sleep 1
end
end

Not a nice solution, but works... Any other idea?
Maybe one in pure Ruby?

s.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top