CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [richsark] need another set of eyes on my perl script: Edit Log



FishMonger
Veteran / Moderator

May 1, 2009, 12:26 PM


Views: 4222
Re: [richsark] need another set of eyes on my perl script

You didn't make the change I suggested.

Try this version of the longer script and post its output as well as the dump.log file it creates. Post the log file as an attachment.


Code
#!/usr/bin/perl 

usw strcit;
use warnings;
use Data::Dumper;

open my $log, '>', 'log-external-.txt' or die "Could not open log: $!";
print $log "Subnet,Static,DHCP,Unused\n";

open my $dump, '>', 'dump.log' or die "failed to open 'dump.log' $!";

##### Step 1, read subnets
open my $in, '<', 'm-names.txt' or die "Could not open m-names.txt: $!\n";
while( my $subnet = <$in>) {

print "Checking $subnet";
chomp $subnet;

my %counts = (
Static => 0,
DHCP => 0,
Unused => 0,
);

my @dnsoptions = `./getobjectlst.exe -u xx -p xx -a $subnet -o Rich`;

print $dump "dumping \@dnsoptions\n";
print $dump Data::Dumper->Dump(\@dnsoptions);
print $dump '=' x 25, "\n";

if ( @dnsoptions and $dnsoptions[0] = '' ) {
print "dnsoptions is null or undefined, going on to the next subnet\n";
next;
}

# Now, at this point, we may have "Error 48" in @dnsoptions, or we may have
# the nicely formatted output. We'll have to check for both cases here.
# Let's check the unsuccessful case first. The condition below checks if
# the first element of @dnsoptions array has the following text in it -
# "Error 48: This subnet does not exist." in it.


if (join("",@dnsoptions) =~ /Error 48: This subnet does not exist./) {

# call "getsubnetlst.exe", passing $subnet as one of the parameters
my @subnetpart2 = `./getsubnetlst.exe -u xx-p xx -a $subnet -t network -o Rich`;

# now loop through each element of the array @subnetpart2, which looks like this -
# ##########################################################################
# "East" "146.149.1.0" "N" "" "146.149.0.0" " " " " "255.255.255.128"
# ##########################################################################
# pick up the 2nd field from the left (e.g. 146.149.1.0 above), and pass it as
# a parameter to the cli "getobjectlst.exe".


foreach my $line ( @subnetpart2 ) {

# get the 2nd field from the left
my $snetpart2 = (split/"\s+"/, $line)[1];

# and now pass it to "getobjectlst.exe";
my @dnsoptions2 = `./getobjectlst.exe -u xx -p xx -a $snetpart2 -o Rich`;

print $dump "dumping \@dnsoptions2\n";
print $dump Data::Dumper->Dump(\@dnsoptions2);
print $dump '=' x 25, "\n";

# find out counts of each subnettype (4th field from left)
foreach my $line (@dnsoptions2) {
my @subnettype = split/"\s+"/, $line;
$counts{$subnettype[3]}++;
}
}
}
else { # successful output from getobjectlst.exe

# find out counts of each subnettype (4th field from left)
foreach my $line (@dnsoptions) {
@subnettype = split/"\s+"/, $line;
$counts{$subnettype[3]}++;
}
}
printf $log "%s,%d,%d,%d\n", $subnet, $counts{Static}, $counts{DHCP}, $counts{Unused};
}

close($in);
close($log);



(This post was edited by FishMonger on May 1, 2009, 12:35 PM)


Edit Log:
Post edited by FishMonger (Veteran) on May 1, 2009, 12:29 PM
Post edited by FishMonger (Veteran) on May 1, 2009, 12:35 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives