ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
*/
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;
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.
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810
Rep:
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.
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
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
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"????
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
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.