Optionally ignore "host/network is down" errors for ping(8)

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Optionally ignore "host/network is down" errors for ping(8)

Vadim Zhukov
Hello all.

Following diff adds new option to ping(8), making it not output
messages "host is down" and "network is down". Very useful when you're
monitoring/fixing routing problems, with ping started in one window, and
you already know that no packets mean problems, and error messages just
spam your window. Works for me for a long time.

Mnemonic for -W is "Wait until this shit comes up". :)

If this goes in, I'll do the same for ping6(8).

--
  Best wishes,
    Vadim Zhukov

A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?


Index: ping.8
===================================================================
RCS file: /cvs/src/sbin/ping/ping.8,v
retrieving revision 1.45
diff -u -p -r1.45 ping.8
--- ping.8 3 Jul 2010 04:44:51 -0000 1.45
+++ ping.8 12 May 2011 22:46:41 -0000
@@ -39,7 +39,7 @@
 .Sh SYNOPSIS
 .Nm ping
 .Bk -words
-.Op Fl DdEefLnqRrv
+.Op Fl DdEefLnqRrvW
 .Op Fl c Ar count
 .Op Fl I Ar ifaddr
 .Op Fl i Ar wait
@@ -192,6 +192,13 @@ Verbose output.
 ICMP packets other than
 .Dv ECHO_REPLY
 that are received are listed.
+.It Fl W
+Do not print
+.Dq Host is down
+or
+.Dq Network is down
+error messages.
+Mnemonic: Wait until it come up.
 .It Fl w Ar maxwait
 Specifies the maximum number of seconds to wait for responses
 after the last request has been sent.
Index: ping.c
===================================================================
RCS file: /cvs/src/sbin/ping/ping.c,v
retrieving revision 1.88
diff -u -p -r1.88 ping.c
--- ping.c 3 Jul 2010 04:44:51 -0000 1.88
+++ ping.c 12 May 2011 22:46:41 -0000
@@ -108,6 +108,7 @@ int options;
 #define F_SO_JUMBO 0x1000
 #define F_AUD_RECV 0x2000
 #define F_AUD_MISS 0x4000
+#define F_NODOWN 0x8000
 
 /* multicast options */
 int moptions;
@@ -201,7 +202,7 @@ main(int argc, char *argv[])
  preload = 0;
  datap = &outpack[8 + sizeof(struct tvi)];
  while ((ch = getopt(argc, argv,
-    "DEI:LRS:c:defi:jl:np:qrs:T:t:V:vw:")) != -1)
+    "c:DdEefI:i:jL:l:np:qRrS:s:T:t:V:vWw:")) != -1)
  switch(ch) {
  case 'c':
  npackets = (unsigned long)strtonum(optarg, 0,
@@ -319,6 +320,9 @@ main(int argc, char *argv[])
  case 'v':
  options |= F_VERBOSE;
  break;
+ case 'W':
+ options |= F_NODOWN;
+ break;
  case 'w':
  maxwait = (unsigned int)strtonum(optarg, 1, INT_MAX,
     &errstr);
@@ -653,11 +657,15 @@ pinger(void)
     sizeof(whereto));
 
  if (i < 0 || i != cc)  {
- if (i < 0)
- perror("ping: sendto");
- snprintf(buf, sizeof buf, "ping: wrote %s %d chars, ret=%d\n",
-    hostname, cc, i);
- write(STDOUT_FILENO, buf, strlen(buf));
+ if (i >= 0 || (errno != ENETDOWN && errno != EHOSTDOWN) ||
+    (options & F_NODOWN) != F_NODOWN) {
+ if (i < 0)
+ perror("ping: sendto");
+ snprintf(buf, sizeof buf,
+    "ping: wrote %s %d chars, ret=%d\n",
+    hostname, cc, i);
+ write(STDOUT_FILENO, buf, strlen(buf));
+ }
  }
  if (!(options & F_QUIET) && options & F_FLOOD)
  (void)write(STDOUT_FILENO, &DOT, 1);
@@ -1363,7 +1371,7 @@ void
 usage(void)
 {
  (void)fprintf(stderr,
-    "usage: ping [-DdEefLnqRrv] [-c count] [-I ifaddr] [-i wait]\n"
+    "usage: ping [-DdEefLnqRrWv] [-c count] [-I ifaddr] [-i wait]\n"
     "\t[-l preload] [-p pattern] [-s packetsize] [-T tos] [-t ttl]\n"
     "\t[-V rtable] [-w maxwait] host\n");
  exit(1);

Reply | Threaded
Open this post in threaded view
|

Re: Optionally ignore "host/network is down" errors for ping(8)

Todd T. Fries-2
Utilities which go into the install media should not be grown without
cause, or at the very least, growth wrapped with #ifndef SMALL.

I think if you want some messages quieted, maybe you should look up
some standard unix utilities.

ping host 2>&1 | awk '/is down/{next}{print}'

Penned by Vadim Zhukov on 20110512 17:54.19, we have:
| Hello all.
|
| Following diff adds new option to ping(8), making it not output
| messages "host is down" and "network is down". Very useful when you're
| monitoring/fixing routing problems, with ping started in one window, and
| you already know that no packets mean problems, and error messages just
| spam your window. Works for me for a long time.
|
| Mnemonic for -W is "Wait until this shit comes up". :)
|
| If this goes in, I'll do the same for ping6(8).
|
| --
|   Best wishes,
|     Vadim Zhukov
|
| A: Because it messes up the order in which people normally read text.
| Q: Why is top-posting such a bad thing?
| A: Top-posting.
| Q: What is the most annoying thing in e-mail?
|
|
| Index: ping.8
| ===================================================================
| RCS file: /cvs/src/sbin/ping/ping.8,v
| retrieving revision 1.45
| diff -u -p -r1.45 ping.8
| --- ping.8 3 Jul 2010 04:44:51 -0000 1.45
| +++ ping.8 12 May 2011 22:46:41 -0000
| @@ -39,7 +39,7 @@
|  .Sh SYNOPSIS
|  .Nm ping
|  .Bk -words
| -.Op Fl DdEefLnqRrv
| +.Op Fl DdEefLnqRrvW
|  .Op Fl c Ar count
|  .Op Fl I Ar ifaddr
|  .Op Fl i Ar wait
| @@ -192,6 +192,13 @@ Verbose output.
|  ICMP packets other than
|  .Dv ECHO_REPLY
|  that are received are listed.
| +.It Fl W
| +Do not print
| +.Dq Host is down
| +or
| +.Dq Network is down
| +error messages.
| +Mnemonic: Wait until it come up.
|  .It Fl w Ar maxwait
|  Specifies the maximum number of seconds to wait for responses
|  after the last request has been sent.
| Index: ping.c
| ===================================================================
| RCS file: /cvs/src/sbin/ping/ping.c,v
| retrieving revision 1.88
| diff -u -p -r1.88 ping.c
| --- ping.c 3 Jul 2010 04:44:51 -0000 1.88
| +++ ping.c 12 May 2011 22:46:41 -0000
| @@ -108,6 +108,7 @@ int options;
|  #define F_SO_JUMBO 0x1000
|  #define F_AUD_RECV 0x2000
|  #define F_AUD_MISS 0x4000
| +#define F_NODOWN 0x8000
|  
|  /* multicast options */
|  int moptions;
| @@ -201,7 +202,7 @@ main(int argc, char *argv[])
|   preload = 0;
|   datap = &outpack[8 + sizeof(struct tvi)];
|   while ((ch = getopt(argc, argv,
| -    "DEI:LRS:c:defi:jl:np:qrs:T:t:V:vw:")) != -1)
| +    "c:DdEefI:i:jL:l:np:qRrS:s:T:t:V:vWw:")) != -1)
|   switch(ch) {
|   case 'c':
|   npackets = (unsigned long)strtonum(optarg, 0,
| @@ -319,6 +320,9 @@ main(int argc, char *argv[])
|   case 'v':
|   options |= F_VERBOSE;
|   break;
| + case 'W':
| + options |= F_NODOWN;
| + break;
|   case 'w':
|   maxwait = (unsigned int)strtonum(optarg, 1, INT_MAX,
|      &errstr);
| @@ -653,11 +657,15 @@ pinger(void)
|      sizeof(whereto));
|  
|   if (i < 0 || i != cc)  {
| - if (i < 0)
| - perror("ping: sendto");
| - snprintf(buf, sizeof buf, "ping: wrote %s %d chars, ret=%d\n",
| -    hostname, cc, i);
| - write(STDOUT_FILENO, buf, strlen(buf));
| + if (i >= 0 || (errno != ENETDOWN && errno != EHOSTDOWN) ||
| +    (options & F_NODOWN) != F_NODOWN) {
| + if (i < 0)
| + perror("ping: sendto");
| + snprintf(buf, sizeof buf,
| +    "ping: wrote %s %d chars, ret=%d\n",
| +    hostname, cc, i);
| + write(STDOUT_FILENO, buf, strlen(buf));
| + }
|   }
|   if (!(options & F_QUIET) && options & F_FLOOD)
|   (void)write(STDOUT_FILENO, &DOT, 1);
| @@ -1363,7 +1371,7 @@ void
|  usage(void)
|  {
|   (void)fprintf(stderr,
| -    "usage: ping [-DdEefLnqRrv] [-c count] [-I ifaddr] [-i wait]\n"
| +    "usage: ping [-DdEefLnqRrWv] [-c count] [-I ifaddr] [-i wait]\n"
|      "\t[-l preload] [-p pattern] [-s packetsize] [-T tos] [-t ttl]\n"
|      "\t[-V rtable] [-w maxwait] host\n");
|   exit(1);

--
Todd Fries .. [hidden email]

 _____________________________________________
|                                             \  1.636.410.0632 (voice)
| Free Daemon Consulting, LLC                 \  1.405.227.9094 (voice)
| http://FreeDaemonConsulting.com             \  1.866.792.3418 (FAX)
| 2525 NW Expy #525, Oklahoma City, OK 73112  \  sip:[hidden email]
| "..in support of free software solutions."  \  sip:[hidden email]
 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
                                                 
              37E7 D3EB 74D0 8D66 A68D  B866 0326 204E 3F42 004A
                        http://todd.fries.net/pgp.txt

Reply | Threaded
Open this post in threaded view
|

Re: Optionally ignore "host/network is down" errors for ping(8)

Alexander Hall-3
On 05/13/11 04:54, Todd T. Fries wrote:

> ping host 2>&1 | awk '/is down/{next}{print}'

"grep -v" was too trivial, was it? ;-D

Reply | Threaded
Open this post in threaded view
|

Re: Optionally ignore "host/network is down" errors for ping(8)

Vadim Zhukov
In reply to this post by Todd T. Fries-2
On 13 May 2011 c. 06:54:04 Todd T. Fries wrote:
> Utilities which go into the install media should not be grown without
> cause, or at the very least, growth wrapped with #ifndef SMALL.

Oops, thanks.

> I think if you want some messages quieted, maybe you should look up
> some standard unix utilities.
>
> ping host 2>&1 | awk '/is down/{next}{print}'

Yes, I'm usually using the following:

 ping 1.2.3.4 2>&1 | fgrep 'bytes from'

But this way I do not see any errors. And your script will not work
because it will allow lines such as:

 ping: wrote 1.2.3.4 64 chars, ret=-1

to be printed too. ;)

Of course, mentioned functionality could be managed as shell script, but
I thought it may be handy to have such functionality in the base.

Anyway, here is an improved version, using your suggestion, for the case
anyone interested.

--
  Best wishes,
    Vadim Zhukov

A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?


Index: ping.8
===================================================================
RCS file: /cvs/src/sbin/ping/ping.8,v
retrieving revision 1.45
diff -u -p -r1.45 ping.8
--- ping.8 3 Jul 2010 04:44:51 -0000 1.45
+++ ping.8 13 May 2011 07:45:09 -0000
@@ -39,7 +39,7 @@
 .Sh SYNOPSIS
 .Nm ping
 .Bk -words
-.Op Fl DdEefLnqRrv
+.Op Fl DdEefLnqRrvW
 .Op Fl c Ar count
 .Op Fl I Ar ifaddr
 .Op Fl i Ar wait
@@ -192,6 +192,13 @@ Verbose output.
 ICMP packets other than
 .Dv ECHO_REPLY
 that are received are listed.
+.It Fl W
+Do not print
+.Dq Host is down
+or
+.Dq Network is down
+error messages.
+Mnemonic: Wait until it come up.
 .It Fl w Ar maxwait
 Specifies the maximum number of seconds to wait for responses
 after the last request has been sent.
Index: ping.c
===================================================================
RCS file: /cvs/src/sbin/ping/ping.c,v
retrieving revision 1.88
diff -u -p -r1.88 ping.c
--- ping.c 3 Jul 2010 04:44:51 -0000 1.88
+++ ping.c 13 May 2011 07:45:09 -0000
@@ -108,6 +108,9 @@ int options;
 #define F_SO_JUMBO 0x1000
 #define F_AUD_RECV 0x2000
 #define F_AUD_MISS 0x4000
+#ifndef SMALL
+#define F_NODOWN 0x8000
+#endif

 /* multicast options */
 int moptions;
@@ -201,7 +204,12 @@ main(int argc, char *argv[])
  preload = 0;
  datap = &outpack[8 + sizeof(struct tvi)];
  while ((ch = getopt(argc, argv,
-    "DEI:LRS:c:defi:jl:np:qrs:T:t:V:vw:")) != -1)
+#ifndef SMALL
+    "c:DdEefI:i:jL:l:np:qRrS:s:T:t:V:vWw:"
+#else
+    "c:DdEefI:i:jL:l:np:qRrS:s:T:t:V:vw:"
+#endif
+    )) != -1)
  switch(ch) {
  case 'c':
  npackets = (unsigned long)strtonum(optarg, 0,
@@ -319,6 +327,11 @@ main(int argc, char *argv[])
  case 'v':
  options |= F_VERBOSE;
  break;
+#ifndef SMALL
+ case 'W':
+ options |= F_NODOWN;
+ break;
+#endif
  case 'w':
  maxwait = (unsigned int)strtonum(optarg, 1, INT_MAX,
     &errstr);
@@ -653,11 +666,19 @@ pinger(void)
     sizeof(whereto));

  if (i < 0 || i != cc)  {
- if (i < 0)
- perror("ping: sendto");
- snprintf(buf, sizeof buf, "ping: wrote %s %d chars, ret=%d\n",
-    hostname, cc, i);
- write(STDOUT_FILENO, buf, strlen(buf));
+#ifndef SMALL
+ if (i >= 0 || (errno != ENETDOWN && errno != EHOSTDOWN) ||
+    (options & F_NODOWN) != F_NODOWN) {
+#else
+ if (i >= 0) {
+#endif
+ if (i < 0)
+ perror("ping: sendto");
+ snprintf(buf, sizeof buf,
+    "ping: wrote %s %d chars, ret=%d\n",
+    hostname, cc, i);
+ write(STDOUT_FILENO, buf, strlen(buf));
+ }
  }
  if (!(options & F_QUIET) && options & F_FLOOD)
  (void)write(STDOUT_FILENO, &DOT, 1);
@@ -1363,7 +1384,12 @@ void
 usage(void)
 {
  (void)fprintf(stderr,
-    "usage: ping [-DdEefLnqRrv] [-c count] [-I ifaddr] [-i wait]\n"
+#ifndef SMALL
+    "usage: ping [-DdEefLnqRrWv]"
+#else
+    "usage: ping [-DdEefLnqRrv]"
+#endif
+                               " [-c count] [-I ifaddr] [-i wait]\n"
     "\t[-l preload] [-p pattern] [-s packetsize] [-T tos] [-t ttl]\n"
     "\t[-V rtable] [-w maxwait] host\n");
  exit(1);

Reply | Threaded
Open this post in threaded view
|

Re: Optionally ignore "host/network is down" errors for ping(8)

Stuart Henderson
On 2011/05/13 11:59, Vadim Zhukov wrote:
> > I think if you want some messages quieted, maybe you should look up
> > some standard unix utilities.
> >
> > ping host 2>&1 | awk '/is down/{next}{print}'
>
> Yes, I'm usually using the following:
>
>  ping 1.2.3.4 2>&1 | fgrep 'bytes from'

> Of course, mentioned functionality could be managed as shell script

This seems like something that could easily be handled with an
shell function, no need for a whole separate script for it:

qping() {
        ping "$@" 2>&1 | fgrep 'bytes from'
}

> I thought it may be handy to have such functionality in the base.

I think something would have to be especially useful to be worth
adding another option flag to ping (especially one which is already
used for something else on other OS)..

FWIW I like to use ping -e / -E for this sort of situation.

Reply | Threaded
Open this post in threaded view
|

Re: Optionally ignore "host/network is down" errors for ping(8)

Todd T. Fries-2
In reply to this post by Alexander Hall-3
Penned by Alexander Hall on 20110513  2:22.36, we have:
| On 05/13/11 04:54, Todd T. Fries wrote:
|
| > ping host 2>&1 | awk '/is down/{next}{print}'
|
| "grep -v" was too trivial, was it? ;-D

sed -n '/is down/{p;}'

Just depends on how granular and what makes most sense to you.

Multiple ways to skin this cat, er paint this bikeshed, etc..
--
Todd Fries .. [hidden email]

 _____________________________________________
|                                             \  1.636.410.0632 (voice)
| Free Daemon Consulting, LLC                 \  1.405.227.9094 (voice)
| http://FreeDaemonConsulting.com             \  1.866.792.3418 (FAX)
| 2525 NW Expy #525, Oklahoma City, OK 73112  \  sip:[hidden email]
| "..in support of free software solutions."  \  sip:[hidden email]
 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
                                                 
              37E7 D3EB 74D0 8D66 A68D  B866 0326 204E 3F42 004A
                        http://todd.fries.net/pgp.txt