Hack and / - Dynamic Config Files with Nmap

Port scans aren't just for script kiddies and network troubleshooting. You also can use them to scan your network for clients and build your server configs dynamically.

The great thing about tools is that you often can misuse them for a
completely different purpose. The end of a screwdriver makes a passable
hammer; a butter knife can be a screwdriver, and even a paper clip can
substitute for a key in a pinch. Normally, you probably think of nmap as a security
tool. After all, it's ideal when you want to test a machine for open,
vulnerable ports. The other day though, I realized nmap had another
use—a
way to scan my network and build a dynamic configuration file based on what
machines replied to my scan.

Munin Is Trendy

This whole project started when I decided to deploy Munin across my servers
so I could graph trending data for each machine on my network. Munin is a
great tool for trending, because once you install the agent, it often
will discover what services and statistics to monitor and graph
automatically.
The downside for me though was that I already had a network full of
servers. It was bad enough that I had to install an agent on each machine,
but I also had to build a giant configuration file on my Munin server by
hand that listed each server it should monitor. Plus, any time I added a
machine to the network, I had yet another step in my build process as I had
to add that new server to my Munin config.

I'm a big fan of automation, and I figured there must be some easier way to
add all my machines to this file. When you look at a Munin configuration
file, it seems ripe for automation:

The syntax for a generic munin.conf file is pretty straightforward. First, a
few directories are defined, and then each server is defined within a pair
of brackets. Inside those brackets, you can assign a name to the server or
just use the hostname. After that, the following line lists the hostname or
IP address for that server. In the above example, I've defined four servers.

If I wanted to generate this configuration file automatically, I had to
figure out some way to detect what servers were running Munin on my
network. Munin makes this simple though, because each server has a Munin
agent listening on port 4949 by default. All I had to do was use nmap to
scan the network and list all the machines that had port 4949 open. I
figured I could parse that output and append it to my munin.conf file,
and then maybe make a vim macro to go through each line and format it.

Nmap with Grepable Output

The first step was to find the right nmap syntax so that it would scan my
network and list all machines that were listening to port 4949. First, I
tried the standard command:

As you can see, for each machine that nmap finds, it lists the IP, whether
the port is open, and even tries to identify the type of machine. Even
though you could grep out the machines with open ports from this output, it
would be quite a pain to parse everything with the multiline
output. Instead, I used the -oG argument to nmap, which tells it to output
in “grepable format”, along with the - argument, which tells it to send that
output to STDOUT. The result was much simpler to parse:

Kyle Rankin is a director of engineering operations in the San Francisco Bay Area, the author of a number of books including DevOps Troubleshooting and The Official Ubuntu Server Book, and is a columnist for Linux Journal.

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.