Over a million developers have joined DZone.

Telnet/netcat: Waiting for a Port to be Open

· DevOps Zone

The DevOps zone is brought to you in partnership with Sonatype Nexus. The Nexus suite helps scale your DevOps delivery with continuous component intelligence integrated into development tools, including Eclipse, IntelliJ, Jenkins, Bamboo, SonarQube and more. Schedule a demo today

On Friday Nathan and I were setting up a new virtual machine and we needed a firewall rule to be created to allow us to connect to another machine which had some JAR files we wanted to download.

We wanted to know when it had been done by one of our operations team and I initially thought we might be able to do that using telnet:

$ telnet 10.0.0.1 8081
Trying 10.0.0.1...
telnet: connect to address 10.0.0.1: Operation timed out
telnet: Unable to connect to remote host

We wanted to put a watch on the command so that it would be repeated every few seconds and indicate when we’d could connect to the port. However, as as far as I can tell there’s no way to reduce the length of the telnet timeout so Nathan suggested using netcat instead.

We ended up with the following command…

$ nc -v -w 1 10.0.0.1 8081
nc: connect to 10.0.0.1 port 8081 (tcp) failed: Connection refused

…which we can then wire up with watch like so:

$ watch "nc -v -w 1 10.0.0.1 8081"
 
Every 2.0s: nc -v -w 1 10.0.0.1 8081                         Sun Jan 20 15:48:05 2013
 
nc: connect to 10.0.0.1 port 8081 (tcp) timed out: Operation now in progress

And then when it works:

Every 2.0s: nc -v -w 1 10.0.0.1 8081                         Sun Jan 20 15:49:53 2013
 
Connection to 10.0.0.1 8081 port [tcp] succeeded!


The DevOps zone is brought to you in partnership with Sonatype Nexus. Use the Nexus Suite to automate your software supply chain and ensure you're using the highest quality open source components at every step of the development lifecycle. Get Nexus today

Topics:

Published at DZone with permission of Mark Needham, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}