Empulse Group a collection of notes from a sys admin, musician, and father

15May/110

apachedump.pl

#!/usr/bin/perl
#
# apachedump.pl
# ver 0.1.0
# Eric Hernandez
# 20110516.1221
#
# Will dump out VirtualHosts found in the Apache httpd.conf file
# as well as looking for Included files and the VirtualHost blocks
# within them.
#

use File::Basename;

my $count = 1;
my $show_includes = 0;
my $show_listen = 0;

# GET THE LAST ARGUMENT AS FILENAME
my $conf_file = $ARGV[$#ARGV];
open (FILE, $conf_file);

# LOAD httpd.conf FILE IN TO @ARRAY
@file_line = <FILE>;
close (FILE);

# START SCRIPT BY PRINTING BANNER AND LOG FILE
print "apachedump.pl\n\n";
#print "Main File: " . $conf_file . "\n";
# CALL TO MAIN FUNCTION
master(@file_line);

#
# SUBROUTINES
#
sub master {

my @line = @_;
my $in_vhost_flag = 0;

for (my $i = 0; $i <= $#line; $i++) {
if ($line[$i] =~ /^Include/) {
# IF AN Include DIRECTIVE IS FOUND and THEN WE NEED TO OPEN THAT FILE
@tmp = split(/ /, $line[$i]);
my $name = "/etc/httpd/" . $tmp[1];
include_file($name);
}
if ($line[$i] =~ /^Listen/ && $show_listen == 1) {
print $line[$i] . "\n";
}
if ($line[$i] =~ /^<VirtualHost/) {
print "(" . $count . ")" . " Virtual Host block found";
my $tmp = $line[$i];
$tmp =~ s/\<//;
$tmp =~ s/\>//;
$tmp =~ s/VirtualHost//;
$tmp =~ s/ //;
chomp $tmp;
print " listening on " . $tmp . "\n";

$in_vhost_flag = 1;
}
if ($line[$i] !~ /^<\/VirtualHost>/ && $in_vhost_flag == 1) {
# HERE WE ARE IN THE VHost BLOCK
# print $line[$i];
if ($line[$i] =~ /[^#](DocumentRoot|ServerName|ServerAlias)/) {
print $line[$i];
}
}
if ($line[$i] =~ /^<\/VirtualHost/) {
# print $line[$i];
$in_vhost_flag = 0;
print "\n";
$count = $count + 1;
}
}

}

sub include_file() {
my $name = $_[0];

# GET FILES IN THE INCLUDED DIRECTORY and SEND TO main() FUNCTION
chomp $name;

opendir (DIR, "/etc/httpd/conf.d/") or die $!;

my $base = basename($name);

$base =~ s/\./\\\./; # TO CHANGE "." TO "\."
$base =~ s/\*/\.\*/; # TO CHANGE "*" TO ".*"
$base = $base . "\$"; # now should be ".*\.conf$"

while (my $file = readdir(DIR)) {
if ($file =~ /$base/) {
my $file = dirname($name) . "/" . $file;
chomp $file;

if ($show_includes == 1) {
print "\nIncluded File: " . $file . "\n";
}

open (INC_FILE, $file) or die "Can't open $f : $!";;
my @include = <INC_FILE>;
close(INC_FILE);
master(@include);
}
}

}
Tagged as: , , No Comments