Jun 112012
 

As a sysadmin, I like to get things done as quickly as possible – that way I can start doing more things. I hate waiting for something to occur, particularly when this means having to constantly context-switch my attention back to the host, checking and rechecking a file or command.

An example of hits is the time I was waiting for a DNS change to propagate downwards to a local server. I got to thinking, ‘why should I constantly type “dig” every 10 minutes? This computer should do the work for me’.

Being inclined to do as little as possible, I wanted an easy single-line shell command – not an entire script. Here’s what I came up with; a piece of bash that would regularly run a command until a given outcome, and then exit and email me.

In this example, I wanted to be emailed when the IP address of www.example.com became “10.10.10.10” in DNS (obviously these examples have been sanitised of all real world corporate information).

Here it is in one line (although separated with line breaks to show the logical components). It’s also nohup’ed for resilience.

# nohup until \
  dig www.example.com|grep -A1 "ANSWER SECTION"|tail -1|grep 10.10.10.10; \
  do sleep 300; done | mail -s "IP Changed" matt@email-address.com &

So this will simply query the IP information with dig, find the line in the ANSWER SECTION and compare it to the expected new IP address. If it exists, the until loop will exit and email me. If the pattern isn’t returned, the loop will sleep for five minutes (300 seconds) before running again.

I setted and forgetted this script and went about my other tasks. About 12 hours later I got an email saying that the IP address had changed. I didn’t have to make any periodic checks.

This sort of thing is a great technique to get your head around. Utilising tricks like this can hugely increase your productivity by taking the drudgery out of your hands and allow you to do get on to more important thinking-heavy jobs.

Any improvements to the command, particularly in how to make it shorter? If so, I’d love to hear about it in the comments.


Matt Parsons is a freelance Linux specialist who has designed, built and supported Unix and Linux systems in the finance, telecommunications and media industries.

He lives and works in London.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>