# Low level TCP port openness check

**URL:** <https://www.ruby-forum.com/t/low-level-tcp-port-openness-check/180639>\
**Category:** Ruby\
**Created:** [December 24, 2009, 6:22pm UTC](https://www.ruby-forum.com/t/low-level-tcp-port-openness-check/180639 "2009-12-24T18:22:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bsanyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7c8e57/32.png) [@bsanyi](https://www.ruby-forum.com/u/bsanyi)\
**Post date:** [December 24, 2009, 6:22pm UTC](https://www.ruby-forum.com/t/low-level-tcp-port-openness-check/180639/1 "2009-12-24T18:22:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bsanyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7c8e57/32.png) [@bsanyi](https://www.ruby-forum.com/u/bsanyi)\
**Post date:** [December 25, 2009, 1:12am UTC](https://www.ruby-forum.com/t/low-level-tcp-port-openness-check/180639/2 "2009-12-25T01:12:17Z")

</div>

Bedo Sandor wrote:

> 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.

---

<div class="post-metadata">

**Author:** ![bsanyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7c8e57/32.png) [@bsanyi](https://www.ruby-forum.com/u/bsanyi)\
**Post date:** [December 25, 2009, 10:28am UTC](https://www.ruby-forum.com/t/low-level-tcp-port-openness-check/180639/3 "2009-12-25T10:28:41Z")

</div>

Albert S. wrote:

> 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.
