Tag Archives: GNU

GNU and UNIX Commands: Using a while Loop to Populate Configuration File

I recently had a need to generate a lot of Nagios host definitions. The hosts all had a similar naming convention:

foo-nnnnprjenv

Here, nnnn was a zero-padded number from 0 to 9999. In this particular environment, there were 41 hosts. A simple multiline echo command inside a while loop fed by seq produces the appropriate host stanzas we can add to our Nagios configuration. We also look up the servers IP addresses via the host command and insert those too:

The -w option to seq causes it to output a leading-zero padded number. Redirect the output of this loop to a file, and you’ll have your Nagios host configuration. This is another reasons why having a naming convention for a large number of servers is important - it will aid you in automation and administration down the track.

GNU and UNIX Commands: Sending HTML-formatted email from the Command Line

Some brief notes I made on sending HTML mail (with an optional attachment) from the command line.

GNU and UNIX Commands: Find Out Which Ports a Program is Listening on

This is easiest done with lsof. On Solaris you can use pfiles, and you may also find netstat -anlp on Linux useful.

First, find the PID of the process you’re interested in

Then, run lsof against the PID

Port 1589/UDP, that’s the information we wanted.

GNU and UNIX Commands: grep with AND instead of OR

We all know how easy it is to grep for something OR something_else:

However, there’s no elegant way to grep for something AND something_else:

Easiest way around this (and most elegant) is with awk:

GNU and UNIX Commands: GNU date and the -d option

If your system has GNU date installed, you can perform some very cool and quick date arithmetic with the -d option. For example:

You get the idea. Read the man and info pages for date for further information!