LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-23-2008, 02:27 AM   #1
please
Member
 
Registered: Apr 2007
Posts: 195

Rep: Reputation: 30
Unhappy Who is can fix?


Dear Sir

who is can fix following tcping.
I am not a programmer so very trouble for fix that
tcping example is tcping <host> port>( tcping 192.168.1.1 53)
I want to use default port 53 without port option for ( tcping 192.168.1.1 ).
I mean is when I type tcping 192.168.1.1, I want to be get result for 53

Please help me sirs
following is tcping code

/*
* $Id: tcping.c,v 1.17 2004/11/02 09:10:10 mkirchner Exp $
*
* tcping.c
*
* Marc Kirchner <mail(at)marc(dash)kirchner(dot)de>
*
* 2003-03-14
*
* use nonblocking connect to test if a port is reachable
* exit codes are:
* -1 an error occured
* 0 port is open
* 1 port is closed
* 2 user timeout
*
* This code may be freely redistributed under the terms of the
* GNU General Public License.
*/

#define VERSION 1.3.4

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include <unistd.h>
#include <sys/time.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <netdb.h>

void usage();

int main (int argc, char *argv[]) {

int sockfd;
struct sockaddr_in addr;
struct hostent *host;
int error = 0;
int ret;
socklen_t errlen;
struct timeval timeout;
fd_set fdrset, fdwset;
int verbose=1;
int c;
char *cptr;
long timeout_sec=0, timeout_usec=0;
int port=0;

if (argc < 3) {
usage(argv[0]);
}

while((c = getopt(argc, argv, "qt:u:")) != -1) {
switch(c) {
case 'q':
verbose = 0;
break;
case 't':
cptr = NULL;
timeout_sec = strtol(optarg, &cptr, 10);
if (cptr == optarg)
usage(argv[0]);
break;
case 'u':
cptr = NULL;
timeout_usec = strtol(optarg, &cptr, 10);
if (cptr == optarg)
usage(argv[0]);
break;
default:
usage(argv[0]);
break;
}
}

sockfd = socket (AF_INET, SOCK_STREAM, 0);

memset(&addr, 0, sizeof(addr));

if ((host = gethostbyname(argv[optind])) == NULL) {
if (verbose)
#ifdef HAVE_HSTRERROR
fprintf(stderr, "error: %s\n", hstrerror(h_errno));
#else
fprintf(stderr, "error: host not found");
#endif
exit(-1);
}

memcpy(&addr.sin_addr, host->h_addr_list[0], host->h_length);
addr.sin_family = host->h_addrtype; /* always AF_INET */
if (argv[optind+1]) {
cptr = NULL;
port = strtol(argv[optind+1], &cptr, 10);
if (cptr == argv[optind+1])
usage(argv[0]);
} else {
usage(argv[0]);
}
addr.sin_port = htons(port);

fcntl(sockfd, F_SETFL, O_NONBLOCK);
if ((ret = connect(sockfd, (struct sockaddr *) &addr, sizeof(addr))) != 0) {
if (errno != EINPROGRESS) {
#ifdef HAVE_SOLARIS
/* solaris immediately returns ECONNREFUSED on local ports */
if (errno == ECONNREFUSED) {
if (verbose)
fprintf(stdout, "%s port %s closed.\n", argv[optind], argv[optind+1]);
close(sockfd);
return(1);
} else {
#endif
if (verbose)
fprintf(stderr, "error: %s port %s: %s\n", argv[optind], argv[optind+1], strerror(errno));
return (-1);
#ifdef HAVE_SOLARIS
}
#endif
}

FD_ZERO(&fdrset);
FD_SET(sockfd, &fdrset);
fdwset = fdrset;

timeout.tv_sec=timeout_sec + timeout_usec / 1000000;
timeout.tv_usec=timeout_usec % 1000000;

if ((ret = select(sockfd+1, &fdrset, &fdwset, NULL, timeout.tv_sec+timeout.tv_usec > 0 ? &timeout : NULL)) == 0) {
/* timeout */
close(sockfd);
if (verbose)
fprintf(stdout, "%s port %s user timeout.\n", argv[optind], argv[optind+1]);
return(2);
}
if (FD_ISSET(sockfd, &fdrset) || FD_ISSET(sockfd, &fdwset)) {
errlen = sizeof(error);
if ((ret=getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &errlen)) != 0) {
/* getsockopt error */
if (verbose)
fprintf(stderr, "error: %s port %s: getsockopt: %s\n", argv[optind], argv[optind+1], strerror(errno));
close(sockfd);
return(-1);
}
if (error != 0) {
if (verbose)
fprintf(stdout, "%s port %s closed.\n", argv[optind], argv[optind+1]);
close(sockfd);
return(1);
}
} else {
if (verbose)
fprintf(stderr, "error: select: sockfd not set\n");
exit(-1);
}
}
/* OK, connection established */
close(sockfd);
if (verbose)
fprintf(stdout, "%s port %s open.\n", argv[optind], argv[optind+1]);
return 0;
}

void usage(char *prog) {
fprintf(stderr, "error: Usage: %s [-q] [-t timeout_sec] [-u timeout_usec] <host> <port> \n", prog);
exit(-1);
}




Best Regards
Please
 
Old 10-23-2008, 11:13 AM   #2
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
Rather than mess with source code why don't you just create a shell script wrapper then alias it to tcping ? Something like this:

Code:
#!/bin/sh
$(which tcping) $1 53
Create the file for, example mytcp.sh, in some directory say /home/me then make it executable with
Code:
chmod +x ./mytcp.sh
then alias it with :

Code:
alias tcping=/home/me/mytcp.sh
Then when you type
Code:
tcping 192.169.1.1
it will call tcping 192.168.1.1 53

Hope this helps.
 
Old 10-23-2008, 10:56 PM   #3
please
Member
 
Registered: Apr 2007
Posts: 195

Original Poster
Rep: Reputation: 30
Hi sir bgeddy



Thanks for your fix.
I got it.
can I do permanently for alias?
I not want to type when OS restart.
can you help me again?

Thanks
Please
 
Old 10-23-2008, 11:05 PM   #4
please
Member
 
Registered: Apr 2007
Posts: 195

Original Poster
Rep: Reputation: 30
Hi sir

Other tcping-0.1 command is

tcping -h www.google.com -p 80

How can I change above command to tcping www.google.com ?

Can you help me also this 1?
 
Old 10-24-2008, 12:07 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
In the future, please post code in [ code ] ... [ /code ] blocks like this:

Code:
/*
* $Id: tcping22.c,v 1.1 2008/10/24 05:05:26 jschiwal Exp jschiwal $
*
* tcping22.c
*
* Marc Kirchner <mail(at)marc(dash)kirchner(dot)de>
*
* 2003-03-14
*
* use nonblocking connect to test if a port is reachable
* exit codes are:
* -1 an error occured
* 0 port is open
* 1 port is closed
* 2 user timeout
*
* This code may be freely redistributed under the terms of the
* GNU General Public License.
*/

#define VERSION 1.3.4

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include <unistd.h>
#include <sys/time.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <netdb.h>

void usage ();

int
main (int argc, char *argv[])
{

  int sockfd;
  struct sockaddr_in addr;
  struct hostent *host;
  int error = 0;
  int ret;
  socklen_t errlen;
  struct timeval timeout;
  fd_set fdrset, fdwset;
  int verbose = 1;
  int c;
  char *cptr;
  long timeout_sec = 0, timeout_usec = 0;
  int port = 0;

  if (argc < 2)
    {
      usage (argv[0]);
    }

  while ((c = getopt (argc, argv, "qt:u:")) != -1)
    {
      switch (c)
        {
        case 'q':
          verbose = 0;
          break;
        case 't':
          cptr = NULL;
          timeout_sec = strtol (optarg, &cptr, 10);
          if (cptr == optarg)
            usage (argv[0]);
          break;
        case 'u':
          cptr = NULL;
          timeout_usec = strtol (optarg, &cptr, 10);
          if (cptr == optarg)
            usage (argv[0]);
          break;
        default:
          usage (argv[0]);
          break;
        }
    }

  sockfd = socket (AF_INET, SOCK_STREAM, 0);

  memset (&addr, 0, sizeof (addr));

  if ((host = gethostbyname (argv[optind])) == NULL)
    {
      if (verbose)
#ifdef HAVE_HSTRERROR
        fprintf (stderr, "error: %s\n", hstrerror (h_errno));
#else
        fprintf (stderr, "error: host not found");
#endif
      exit (-1);
    }

  memcpy (&addr.sin_addr, host->h_addr_list[0], host->h_length);
  addr.sin_family = host->h_addrtype;   /* always AF_INET */
  if (argv[optind + 1])
    {
      cptr = NULL;
      port = strtol (argv[optind + 1], &cptr, 10);
      if (cptr == argv[optind + 1])
        usage (argv[0]);
    }
  else
    {
      port = 22;
    }
  addr.sin_port = htons (port);

  fcntl (sockfd, F_SETFL, O_NONBLOCK);
  if ((ret = connect (sockfd, (struct sockaddr *) &addr, sizeof (addr))) != 0)
    {
      if (errno != EINPROGRESS)
        {
#ifdef HAVE_SOLARIS
/* solaris immediately returns ECONNREFUSED on local ports */
          if (errno == ECONNREFUSED)
            {
              if (verbose)
                fprintf (stdout, "%s port %s closed.\n", argv[optind],
                         argv[optind + 1]);
              close (sockfd);
              return (1);
            }
          else
            {
#endif
              if (verbose)
                fprintf (stderr, "error: %s port %i: %s\n", argv[optind],
                         port, strerror (errno));
              return (-1);
#ifdef HAVE_SOLARIS
            }
#endif
        }

      FD_ZERO (&fdrset);
      FD_SET (sockfd, &fdrset);
      fdwset = fdrset;

      timeout.tv_sec = timeout_sec + timeout_usec / 1000000;
      timeout.tv_usec = timeout_usec % 1000000;

      if ((ret =
           select (sockfd + 1, &fdrset, &fdwset, NULL,
                   timeout.tv_sec + timeout.tv_usec > 0 ? &timeout : NULL)) ==
          0)
        {
/* timeout */
          close (sockfd);
          if (verbose)
            fprintf (stdout, "%s port %i user timeout.\n", argv[optind], port);
          return (2);
        }
      if (FD_ISSET (sockfd, &fdrset) || FD_ISSET (sockfd, &fdwset))
        {
          errlen = sizeof (error);
          if ((ret =
               getsockopt (sockfd, SOL_SOCKET, SO_ERROR, &error,
                           &errlen)) != 0)
            {
/* getsockopt error */
              if (verbose)
                fprintf (stderr, "error: %s port %i: getsockopt: %s\n",
                         argv[optind], port, strerror (errno));
              close (sockfd);
              return (-1);
            }
          if (error != 0)
            {
              if (verbose)
                fprintf (stdout, "%s port %i closed.\n", argv[optind], port );
              close (sockfd);
              return (1);
            }
        }
      else
        {
          if (verbose)
            fprintf (stderr, "error: select: sockfd not set\n");
          exit (-1);
        }
    }
/* OK, connection established */
  close (sockfd);
  if (verbose)
    fprintf (stdout, "%s port %i open.\n", argv[optind], port);
  return 0;
}

void
usage (char *prog)
{
  fprintf (stderr,
           "error: Usage: %s [-q] [-t timeout_sec] [-u timeout_usec] <host> <port> \n",
           prog);
  exit (-1);
}
Note, that I made changes to default to port 22. I wouldn't replace the real tcping command with this one. Maybe tcping35 or tcping_port35 would be better names. However, using alias or a wrapper would be better. This kind of thing can use a wrapper or alias instead.

For your last question, since the -p option is used, you can alias the command directly like alias='tcping -p 35'. Because the first version used "<address> <port>" you can't alias it to use port 35 because the address comes first. You can alias it to run a wrapper program as the post #2 suggested.
 
Old 10-24-2008, 03:41 AM   #6
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
Quote:
can I do permanently for alias?
Having created /home/me/mytcp.sh wrapper as per my previous post add this to your /etc/profile so every user has access or look into adding it to ~/.bash_profile so just your user has it :

Code:
alias tcping=/home/me/mytcp.sh
alias googletcping=$(which tcping) -h www.google.com -p 80
Now typeing tcping host will actually call tcping host 53 and typing googletcping withh call tcping -h www.google.con -p 80.

All this presumes you run the bash shell - other shells will behave diferently. Check the man page for startup options for your shell.

Last edited by bgeddy; 10-24-2008 at 03:43 AM.
 
Old 10-24-2008, 05:18 AM   #7
please
Member
 
Registered: Apr 2007
Posts: 195

Original Poster
Rep: Reputation: 30
Thanks sir bgeddy

Sir Guru jschiwal

You fixing is very good.
I want to request you one thing.
This is

can u check for tcping

[root@JCLINUX ~]# tcping 192.168.17.100
No hostname given
tcping, (C) 2003 folkert@vanheusden.com

-h hostname hostname (e.g. localhost)
-p portnr portnumber (e.g. 80)
-c count how many times to connect
-i interval delay between each connect
-f flood connect (no delays)
-q quiet, only returncode

[root@JCLINUX ~]#
======================

[root@JCLINUX ~]# tcping -h 192.168.17.100 -p 445
PING 192.168.17.100:445
connected to 192.168.17.100:445, seq=0 time=0.46 ms
connected to 192.168.17.100:445, seq=1 time=0.25 ms
connected to 192.168.17.100:445, seq=2 time=0.25 ms
connected to 192.168.17.100:445, seq=3 time=0.25 ms
connected to 192.168.17.100:445, seq=4 time=0.25 ms
connected to 192.168.17.100:445, seq=5 time=0.25 ms
--- 192.168.17.100:445 ping statistics ---
6 connects, 6 ok, -0.00% failed
round-trip min/avg/max = 0.3/0.3/0.5 ms
[root@JCLINUX ~]#

================================
above is tcping-0.1 version

If I want to see (connected to 192.168.17.100:445, seq=0 time=0.46 ms )
with just type tcping 192.168.17.100
How can I do sir?
Don`t mind me for this .c file because of I have not

Best Regards
Please
 
Old 10-27-2008, 03:50 AM   #8
please
Member
 
Registered: Apr 2007
Posts: 195

Original Poster
Rep: Reputation: 30
Hi sirs

Pls help me for that
Following is tcping-0.1 source code

http://www.vanheusden.com/Linux/tcping-0.1.tgz

Best Regards
Please
 
Old 10-27-2008, 06:42 AM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
The way I read this, you don't want to have to type "-h" when using tcping, and you want to modify the source code to fix it.

Several questions come to mind:
Why is this a problem?
Why not use "ping" instead of "tcping"
How about an alias?

If you are serious about modifying the source code, then you will simply have to find the code that parses the flags.
 
Old 10-27-2008, 09:45 AM   #10
please
Member
 
Registered: Apr 2007
Posts: 195

Original Poster
Rep: Reputation: 30
Unhappy

Hi pix

Thanks for your asking.
I am trying load balancing with vyatta what is base on ping.
Our ISP is deny ping for virus attack.so I have trouble
Now I am trying replace ping to tcping that is why

If you can,Pls help me for that

Best Regards
Please
 
Old 10-27-2008, 10:24 AM   #11
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by please View Post
Hi pix

Thanks for your asking.
I am trying load balancing with vyatta what is base on ping.
Our ISP is deny ping for virus attack.so I have trouble
Now I am trying replace ping to tcping that is why

If you can,Pls help me for that

Best Regards
Please
The questions were

"Why is typing -h a problem" and
"Why can't you alias the command"????
 
Old 10-27-2008, 10:24 PM   #12
please
Member
 
Registered: Apr 2007
Posts: 195

Original Poster
Rep: Reputation: 30
alias is not work when system start up.
vyatta is only allow for ping.

They was used connection health status with ping.
 
Old 10-29-2008, 05:51 AM   #13
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
A word coming in edgewise: it may be of interest to learn about netcat's -z option. And, if you fall in love with netcat, it turns out that socat is even awesomer. Hope that's of some use to someone
 
Old 12-15-2008, 04:12 AM   #14
please
Member
 
Registered: Apr 2007
Posts: 195

Original Poster
Rep: Reputation: 30
Hi jschiwal

U fixed for me tcping.Now I am again facing with problem
This is ping command option have -I .tcping not have
you can fix for me again this Interface option?

Best Regards
Please
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
FIX---Fedora install hangs---To fix this---- jmckinzie Fedora - Installation 0 12-20-2005 12:08 AM
all attempts to fix the problem failed... can someone help me fix partition space? foreverdita Linux - Enterprise 2 05-11-2005 09:02 AM
reiserfsck --fix-fixable -> --fix-fixable ignored - why? BrianK Linux - Software 1 01-07-2005 08:45 PM
How do i fix this? glitch Linux - Newbie 22 09-21-2002 08:08 AM
is it possible or how do i fix this Jasper33 General 3 03-12-2001 06:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration