Operating System - HP-UX
1849236 Members
2737 Online
104042 Solutions
New Discussion

TCP Socket connection checking.

 
SOLVED
Go to solution
Charles Harris
Super Advisor

TCP Socket connection checking.

Dear all,

Does anyone know of a reliable and preferably easy method of checking port status from within a script. I know this can be done from telneting to a port, although automating it from a script seems to produce mixed results.

I've routed out some C code that sounds like it would do the trick perfectly, although it doesn't want to compile and I could write all I know about C on the back of a postage stamp.

Any pointers, tips suggestions warmly received (as ever!!)

Cheers,

-ChaZ-

#include
#include
#include
#include

#define DEST_IP "1.1.1.1" // changed IP
#define DEST_PORT 1022

int main()
{
int sockfd;
struct sockaddr_in dest_addr; // will hold the destination addr

sockfd = socket(AF_INET, SOCK_STREAM, 0); // do some error checking!

dest_addr.sin_family = AF_INET; // host byte order
dest_addr.sin_port = htons(DEST_PORT); // short, network byte order
dest_addr.sin_addr.s_addr = inet_addr(DEST_IP);
memset(&(dest_addr.sin_zero), '\0', 8); // zero the rest of the struct

// don't forget to error check the connect()!
connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr));
}
6 REPLIES 6
Christopher Caldwell
Honored Contributor

Re: TCP Socket connection checking.

You can either finish your C program, or use something like nmap in a script:

"A utility for port scanning large networks. It works fine for single hosts. It incorporates virtually every scanning technique, but specifically it supports TCP connect scanning, TCP SYN and FIN (stealth), bounce attack scanning, ICMP and ping scanning and yet more ... "

http://hpux.cs.utah.edu/hppd/hpux/Networking/Admin/nmap-2.53/
Kofi ARTHIABAH
Honored Contributor
Solution

Re: TCP Socket connection checking.

Another really easy way to check is as follows:

echo "^]close" | telnet IP.ADD.OF.HOST PORT
status=$?

#status = 0 means host is listening on TCP port PORT
#status = 1 means host port is closed on that host.

by the way, in order to generate the telnet escape character, you have to use:
ctrl+v followed by ctrl+]
it is not visible on a command line but in vi it will show as an ^]

good luck.
BTW I am assuming that the status checking you want done is to determine wheather or not it is listening on the port right? this does not necessarily mean that the daemon is DOING what it is supposed to.
nothing wrong with me that a few lines of code cannot fix!
Charles Harris
Super Advisor

Re: TCP Socket connection checking.

Thanks for the nice port scan on a line Kofi!!! - Perfect, exactly what I was after!!!

Thanks again,


-ChaZ-

Ps. I'm well versed in the art of NMAP, although our network boys and security auditors aren't keen on having the app freely availible on our production servers!!! - Thanks all the same!!
Craig Rants
Honored Contributor

Re: TCP Socket connection checking.

I know points have been assigned, but thought I would through my two cents in.

Socket connections can be determined by running netstat -an, it will show udp and tcp information as well. This may be a suitable option for some to telneting to a port, plus you can check udp traffic.

C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
Christopher Caldwell
Honored Contributor

Re: TCP Socket connection checking.

I thought you didn't want to telnet to the port :-(.

Charles Harris
Super Advisor

Re: TCP Socket connection checking.

Hi,

Thanks for the additional comments, Christopher, - sorry, I should have made my question clearer, I had no objection to telnet other than I didn't want to pipe seperate files into the command to get it to check the ports (bit messy) the GURU style ctrl key combo was exactly what I was after! - Thanks for your comments though!

Craig, cheers, netstat -an is okay locally, but this was for a remote checking exercise, so the telnet option is a better bet. Again thanks for the support!!

Cheers,

-ChaZ-