Connection testing with Perl
Since I'm seldom in control of all the devices between myself and whatever system I am trying to reach, I often like to verify whether I will be able to connect to a particular port on the particular system before I concern myself with whether my connection is behaving as it should. For example, I can use a command such as "telnet remhost 9999" and then try to determine if the response I am getting is the one I was expecting. If I have appropriate access to both systems, however, I can start a process on the particular port I want to reach and then run a test to see whether I can reach that process from the other system.
A little expertise with socket I/O can come in very handy for this kind of testing. You can get by, however, with a short but handy sample Perl script like one that I have used for many such tests over the years. I call my script, derived from many Perl command examples that I've encountered on the web, "listen". Listen will open any (non-busy) port above 1023 for a normal user and any (non-busy) port for root. It requires the Socket module for the muscle work and for definitions of most of the variables used (e.g., SOCK_STREAM).
The port that you want to open must be passed as an argument or the script will complain and immediately exit.
To test a port, you would type "./listen 9999" or something like that on one system and "telnet localhost 9999" on the same server or "telnet remhost 9999" (replacing "remhost" with the actual host name of the target system) on a remote server.
Sign up for ITworld's Daily newsletter
Follow ITworld on Twitter @IT_world
Esther Schindler
If the comments are ugly, the code is ugly
claird
SVG a graphics format for 21st century
pasmith
Take Chrome OS for a test spin
Sandra Henry-Stocker
Solaris Tip: Have Your Files Changed Since Installation?
jfruh
Android fragments vs. the iPhone monolith
mikelgan
What Gizmodo missed about the Pro WX Wireless USB disk drive
Sidekick: The Good News & the Bad News
Either way you look at it Microsoft Data Center management did not follow standards or best practices in this failure. In which case it makes me wonder more about the outsourcing of corporate data much less personal data.
- mburton325
Join the conversation here
Quick, practical advice for IT pros. Made fresh daily.
Want to cash in on your IT savvy? Send your tip to tips@itworld.com. If we post it, we'll send you a $25 Amazon e-gift card.














nc
Nice script, but i find myself using netcat for this kind of thing:remote machine: 'nc -lp 9999'
local machine: 'telent remote 9999'
you should see a connection establish and then you will be able to type into the session and whatever you type will appear on the remote netcat session. you could also pipe a file into nc and it will be echoed in your telnet session:
'nc -lp 999 < /etc/motd'
This works on any machine with netcat installed (which I think should be default)
I hope this will be useful to you at some point sandra.
Especially useful on Solaris and the like
Yes, nc or netcat is a powerful utility which can do the same and much more and will be my first choice on Linux systems. However, nc is not available by default on Solaris and if you have constraints which prevent you from installing nc (for example, some crazy company policy), then this script is very handy. Thanks Sandra.