Hack and / - Dynamic Config Files with Nmap
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.
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:
dbdir /var/lib/munin
htmldir /var/www/munin
logdir /var/log/munin
rundir /var/run/munin
tmpldir /etc/munin/templates
[web1.example.net]
address web1.example.net
[web2.example.net]
address web2.example.net
[db1.example.net]
address db1.example.net
[db2.example.net]
address db2.example.net
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.
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:
$ nmap -p 4949 10.1.1.0/24 Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) ↪at 2010-03-01 20:18 PST Interesting ports on 10.1.1.1: PORT STATE SERVICE 4949/tcp closed unknown MAC Address: 00:00:0C:01:CD:05 (Cisco Systems) Interesting ports on purple1.example.net (10.1.1.50): PORT STATE SERVICE 4949/tcp closed unknown MAC Address: 08:00:20:CF:9D:D7 (SUN Microsystems) Interesting ports on web1.example.net (10.1.1.53): PORT STATE SERVICE 4949/tcp open unknown MAC Address: 00:50:56:92:34:02 (VMWare) Interesting ports on web2.example.net (10.1.1.67): PORT STATE SERVICE 4949/tcp open unknown MAC Address: 00:30:48:A0:12:98 (Supermicro Computer) . . .
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:
$ nmap -oG - -p 4949 10.1.1.0/24 # Nmap 4.11 scan initiated Mon Mar 1 20:26:45 2010 as: ↪nmap -oG - -p 4949 # 10.1.1.0/24 Host: 10.1.1.1 () Ports: 4949/closed/tcp///// Host: 10.1.1.50 (purple1.example.net) Ports: 4949/closed/tcp///// Host: 10.1.1.53 (web1.example.net) Ports: 4949/open/tcp///// Host: 10.1.1.67 (web2.example.net) Ports: 4949/open/tcp///// . . .
Now I could just grep for “open”, and I'd get a list of all machines running Munin:
$ nmap -oG - -p 4949 10.1.1.0/24 | grep open Host: 10.1.1.53 (web1.example.net) Ports: 4949/open/tcp///// Host: 10.1.1.67 (web2.example.net) Ports: 4949/open/tcp/////
Kyle Rankin is a systems architect; and the author of DevOps Troubleshooting, The Official Ubuntu Server Book, Knoppix Hacks, Knoppix Pocket Reference, Linux Multimedia Hacks, and Ubuntu Hacks.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- Designing Electronics with Linux
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- Build a Skype Server for Your Home Phone System
- New Products
- Why Python?
- A Topic for Discussion - Open Source Feature-Richness?
- Validate an E-Mail Address with PHP, the Right Way
- Tech Tip: Really Simple HTTP Server with Python
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




1 hour 42 min ago
4 hours 12 min ago
14 hours 15 min ago
18 hours 42 min ago
22 hours 17 min ago
22 hours 50 min ago
1 day 1 hour ago
1 day 1 hour ago
1 day 1 hour ago
1 day 5 hours ago