Hack and / - Dynamic Config Files with Nmap
Once I started working on the regular expressions to parse through this output and generate the syntax I needed, I realized I should ditch vim and just write a script that built the entire configuration file for me and run that script with cron. That way, I'd never have to add a new server again. The only challenge was that I had multiple subnets I wanted to scan, and I discovered that sometimes nmap didn't resolve the IP addresses into hostnames for me. Listing 1 shows the resulting script.
Listing 1. Script to Build Configuration File
#!/usr/bin/perl
my $munin_dir = '/etc/munin';
my $munin_config = 'munin.conf';
my $munin_config_temp = 'munin.conf.tmp';
my $node_port = '4949';
my $nmap = "nmap -oG - -p ";
my %subnets = (
"10.1.1.0/24" => "VLAN1",
"10.1.5.0/24" => "VLAN5",
"10.1.6.0/24" => "VLAN6",
);
# iterate through each subnet and perform the nmap scan
foreach $subnet (keys %subnets){
open NMAP, "$nmap $node_port $subnet | grep open |"
↪or die "Can't run nmap: $!\n";
while (<NMAP>){
$ip = $host = "";
# parse out the hostname and IP address
/Host: (\d+\.\d+\.\d+\.\d+) \((.*?)\)/;
$ip = $1; $host = $2;
next if($ip eq "");
# sometimes nmap doesn't do rDNS properly,
# get it manually in that case
if($host eq ""){
$host = `dig -x $ip +short` or $host = $ip;
chomp $host;
$host =~ s/\.$//;
}
$munin_hosts{$host} = $ip;
}
close NMAP;
}
# output to a temp file in case munin
# runs while this script is open
open OUTFILE, "> $munin_dir/$munin_config_temp" or die "Can't open
$munin_dir/$munin_config_temp: $!\n";
# first print out the standard header for the munin file
print OUTFILE <<END_HEAD;
dbdir /var/lib/munin
htmldir /var/www/munin
logdir /var/log/munin
rundir /var/run/munin
tmpldir /etc/munin/templates
END_HEAD
# then print out the config for each host
foreach $host (sort keys %munin_hosts){
print OUTFILE "\[$host\]\n\taddress $host\n";
# add any extra munin options for each host here
print OUTFILE "\n";
}
close OUTFILE;
system("mv $munin_dir/$munin_config_temp $munin_dir/$munin_config");
Other than the hashes and a little fun with regular expressions, the bulk of this script is basic Perl. Once I tested it a few times by hand and was comfortable with it, I went ahead and copied the script into /etc/cron.daily. Of course, on my real network, I've added a few other fancy touches. For instance, every server on my network has a DNS TXT record that says what the server does. It is a useful practice for many reasons, but in this case, I found that because I used the same TXT record for similar servers, I could look it up and use that to group servers together under that heading.
Although this script worked great for Munin configs, you also could use the same procedure to scan for any number of services and build a configuration. I could see scripts that generate configuration files for Nagios, programs that poll SNMP or any other program that monitors multiple servers over a known port.
Kyle Rankin is a Systems Architect in the San Francisco Bay Area and the author of a number of books, including The Official Ubuntu Server Book, Knoppix Hacks and Ubuntu Hacks. He is currently the president of the North Bay Linux Users' Group.
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.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
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
| 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 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- RSS Feeds
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- New Products
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- Validate an E-Mail Address with PHP, the Right Way
- Trying to Tame the Tablet
- Tech Tip: Really Simple HTTP Server with Python
- New Products
Enter to Win an Adafruit Prototyping Pi Plate 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 Prototyping Pi Plate 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
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
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.




4 hours 11 min ago
4 hours 33 min ago
4 hours 44 min ago
4 hours 48 min ago
5 hours 18 min ago
8 hours 9 min ago
8 hours 45 min ago
8 hours 46 min ago
8 hours 47 min ago
8 hours 48 min ago