This page basically describes how to setup and configure client and server network socket testing. Its basically to show how one can test that ports are open from client to server, before an application installation. Since this is just a script, its much like running "ls" or "grep" or any ordinary tool on a Unix/Linux system - and as such wouldnt require any change control's to be filed
Our specific case here, is to test that an existing server, has connectivity from an existing client. Both of these hosts - client and server - are in use today, and require proper change management documentation to proceed with any installation of software.
So what we needed to do, was to check that the ports are open, so we dont lose even more time. But not being able to install software in production servers presents a bit of a contradiction. Thats where this script was created for.
In order for us to be able to test an installation of an application on the server, using different ports than the ones already installed - we setup a perl script that just listens on a socket. This is not a configuration change to the server - merely a troubleshooting command that doesnt impact running services. This is akin to running any other standard script on the server. So before we install the software, we can properly check to be sure that our ports would be open. So this is a quick Perl socket listener, that can be used in combination with the ping_port program to ensure connectivity is present.
Just copy this code, save it as "perl_socket_8151.pl" (for example, since its port 8151 here) and run it on the command line, in the foreground, so you can kill it when done testing.
#!/usr/bin/perl
#
#####
##### Written by Paul A. Luzzi on 07/01/2008
#####
##### Name : perl_socket_8151.pl
#####
##### Purpose : a basic perl socket listener
##### used to check that firewall ports are
##### open before installation of software
#####
#####
##### Setup our use statements
#####
use strict;
use Socket;
#####
##### Use port of 8151
#####
my $port = shift || 8151;
my $proto = getprotobyname('tcp');
#####
##### Create a socket, make it reusable
#####
socket(SOCKET, PF_INET, SOCK_STREAM, $proto)
or die "Can not open socket $!\n";
setsockopt(SOCKET, SOL_SOCKET, SO_REUSEADDR, 1)
or die "Can not set socket optoin to SO_REUSEADDR $!\n";
#####
##### Bind to a port, then listen
#####
##
## Only works on Linux it seems ...
##
# bind( SOCKET, pack( 'Sn4x8', AF_INET, $port, "0000" ))
# or die "Can not bind to port $port! \n";
##
## This worked on Solaris
##
# my $internetPackedAddress = pack('S n A4 x8', AF_INET(), $port, 0.0.0.0) ;
# bind( SOCKET, $internetPackedAddress )
# or die "Can not bind to port $port! \n";
##
## This works on all now
##
bind( SOCKET, pack('SnA4x8', AF_INET, $port, 0.0.0.0))
or die "Can not bind to port $port! \n";
listen (SOCKET, 5) or die "listen: $!";
print "SERVER started on port $port \n";
#####
##### Accepting a connection !
#####
my $client_addr;
while ($client_addr = accept(NET_SOCKET, SOCKET)) {
# send them a message, close connection
print NEW_SOCKET "Smile from the server :-) ";
close NEW_SOCKET;
}
#####
##### The end
#####
Very important step !! Be sure that there isn't already something running on that port on the server. Port 8151 should be open already in our example here, so this shouldn't be an issue, but you never know. Trust nothing, confirm everything. After that, we can setup the steps to run, test, confirm, etc.
| Task | Location | Command | Description |
|---|---|---|---|
| 1 | server | netstat -an | grep 8151 | Confirm that nothing is already up on port 8151 |
| 2 | client | ping_port.pl server_hostname 8151 | Begin testing against the listener - it should be responding with "Inactive" at this time - of course you need to put your own server's hostname here. |
| 3 | server | perl_socket_8151.pl | Start the listener on the server side and make sure it reports back without error |
| 4 | client | ping_port.pl | Technically, this should still be running, so its not really a new task - but now it should be showing "Active" instead. That signifies that the listener is up, AND that we have connectivity. |
| 5 | server | CTRL-C | Break out of the socket listener, now that we have our answer. |
| 6 | client | CTRL-C | Break out of the client test program, now that we have our answer. |
So our first task, on the server side, just run "netstat -an | grep 8151" to be sure there isn't already something running on that port. Assuming there is not, then we know we're good to proceed. So we just jump over to the client side and run the test client program against our server.
Go to the client machine and run the ping_port.pl program. If its not available on that machine already, just copy and paste since you only need the client perl script. Its a basic Perl script and can run anywhere that Perl is installed. So just run ping_port.pl against the server's address, port 8151. It should be reporting "InActive" initially.
Once thats setup, go back to the server, and run the listener program - and it should show that the server is now listening. This program doesn't do much more than listen for connections, so we can test connectivity - thats all its for. So running that on the server side, and running in foreground, you'll see a message about it listening.
Now, what I described above as the order of steps is not totally critical, but helps with the understanding of whats happening, and when. Also makes it much easier to see the progress, when status should change, etc. Otherwise its harder to understand, at least for me. So what I'd recommend, is to start the ping_port script first - so that you get "inactive" back first - THEN start the server listener on the server, and you should see the ping port go active now, then when killing the listener, the ping port goes inactive again. If that happens, then all connectivity is there, and things are all good. If not, then a firewall request is needed.
Both of these Perl scripts should work on Unix, Linux and even Windows. To use on windows, just remove the first line of each - that has the shell declarative in it..
As the whole purpose of this, is to test that ports are open, if you find they are not, a firewall request needs to be submitted. I'm not sure of the current process, and do not pretend to be the owner of it - so that would need to be verified with that team.
Other assumptions here - if you are not familiar with Perl, these still should be usable. The design is that anyone can cut and paste, and run the commands - and even interpret the output. This is far easier (in my opinion) than using the telnet testing method.
Also added ping_port.pl
page as well.