Small Mosaic


Categories:

/books
/career
/codinghorrors
/events
/geekstuff
/justdont
/languages
/languages/bash
/linkshot
/magazines
/meta
/misctech
/movies
/nottech
/operatingsystems
/operatingsystems/linux
/operatingsystems/linux/debian
/operatingsystems/solaris
/perl
/presentations
/programming
/python
/ruby
/security
/security/apache
/security/tools
/serversmells
/services
/services/dns
/sites
/specifications
/sysadmin
/testing
/tools
/tools/commandline
/tools/firefox
/tools/gui
/tools/network
/tools/online
/tools/online/greasemonkey
/tools/puppet
/unixdaemon

Archives:

July 20111
June 20112
May 20113
April 20112
March 20117
January 20111
December 20103
November 20103
August 20101
July 20101
June 20104
May 20102
April 20101
March 20108
February 20101
January 20102
Full Archives

Thu, 01 Feb 2007

Ping The Host Table - UGU Tip
I'm not too keen on yesterdays UGU tip of the day and it doesn't take much to make it work a chunk better, so I thought I'd whine about it on my blog.

Here's the original snippet:

  
grep -v "#" /etc/hosts | awk '{print $1}' | while read host
do
  ping -c 1 $host
done
  

But this has some very fixable caveats. It doesn't deal with blank lines, it'll try and ping IPv6 addresses (and too many distros put IPv6 entries in the host table these days - even if you disable the IPv6 options) and it will ignore any lines that have a comment, even if the comment is after the field we want. So I wrote my own version (which I can't see me ever using)

  
for host in `awk '! /^#|^$|::/ { print $1 }' /etc/hosts`
do
  ping -c 1 $host
done
  

Mine does deal with blank lines (^$), only drops a comment if it's at the start of the line (^#) and skips all IPv6 addresses (::). If you want to golf it down some more you can even kill both the loop and iterator variable and use xargs instead. But I'm not that bored.

Like this post? - Digg Me! | Add to del.icio.us! | reddit this!

Posted: 2007/02/01 07:01 | /tools/commandline | Permanent link to this entry | This entry and same date


books career codinghorrors events geekstuff justdont magazines meta misctech movies nottech operatingsystems/linux operatingsystems/linux/debian operatingsystems/solaris perl programming python ruby security security/apache security/tools serversmells services/dns sites sysadmin testing tools tools/commandline tools/firefox tools/gui tools/network tools/online tools/online/greasemonkey tools/puppet unixdaemon

Copyright © 2000-2010 Dean Wilson XML feed logo