3 ms·
There you go :) #include <string.h> #include <stdio.h> #include <unistd.h> #include <netdb.h> int main(int argc, char ** argv) { struc
by huhtenberg 17y ago
There you go :)
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
int main(int argc, char ** argv)
{
struct hostent * he;
struct sockaddr_in sa = { AF_INET };
unsigned int port;
int foo, sk;
if (argc != 3)
return 1; /* Syntax error */
if (sscanf(argv[2], "%u%n", &port, &foo) != 1 ||
strlen(argv[2]) != foo ||
port > 65535)
return 1; /* Syntax error */
he = gethostbyname(argv[1]);
if (! he)
return 2; /* DNS error,OS error or syntax error */
sa.sin_addr.s_addr = *(unsigned long*)he->h_addr;
sa.sin_port = htons(port);
sk = socket(AF_INET, SOCK_STREAM, 0);
if (sk < 0)
return 3; /* OS error */
if (connect(sk, (void*)&sa, sizeof sa) < 0)
return 4; /* TCP error */
return 0;
}
To build:
gcc tcp-check.c -o tcp-check
To use:
./tcp-check yahoo.com 80; echo $?
0 is OK, 1 - syntax error, 2 - DNS error, 3 - OS error, 4 - TCP error. Call it from a shell script, look at the return value and, if it's non-zero, dispatch your message. Then stick the script itself into a cron schedule, and you are done.
- jacquesm 17y agoThank you very much for the effort :)
- spudlyo 17y agoIt doesn't support timeouts.
- huhtenberg 17y agoDefine "timeout".