TEXT

Tuesday, November 4, 2008

Simple "nsping"

there's no need to install a separate utility to run "nsping", you have "dig" already ...

A simple network diagnostic tool to determine the health and reachability of name servers is nsping. as the name suggests you ping a name server, not with an ICMP echo request but with a (random) lookup. the time interval it takes to get a reply back is what you're after. serious lags can indicate network issues. the tool nsping is a standalone binary, and on most UN*X systems it's another package to install. however, almost everyone already has dig installed, part of the BIND package. dig is a complex name server query and diagnostic tool. one useful feature of it is that it reports the amount of time it takes to perform it's query in milliseconds ... exactly what we're after. so, let's use dig to do exactly what nsping does and trim down the answer to look like nsping. all we have to do is a random lookup and report only the query time. this simple shell hack uses the built in random number generator from ksh (i think the tool can work in bash, too) and awk to trim down the query from dig.
#!/bin/ksh
SERVER=$1
dig @${SERVER} -t a ${RANDOM}.${RANDOM}${RANDOM}.${RANDOM} | \
        awk '{if ($0 ~/Query/) print $4" "$5}'
put this in your path and ping a name server:
$ nsping ns.oreilly.com     
97 msec
i have used this in the past to query the root nameservers as a network connectivity monitor (with much success).
See also: man dig
"dns & bind", the book from o'reilly
"sed & awk", another book from o'reilly

[Contributed by: jose nazario]

No comments: