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: need another set of eyes on my perl script: Edit Log



richsark
User

May 1, 2009, 6:33 AM


Views: 4744
need another set of eyes on my perl script

This is the code I am working on:

#!perl -w
my %counts;
my @subnettype;
my @dnsoptions;
my @dnsoptions2;
my @subnetpart2;
my $snetpart2;
my $subnet;
open(my $log, ">log-external-.txt") or die "Could not open log: $!\n";
printf $log "Subnet,Static,DHCP,Unused\n";
##### Step 1, read subnets
open(my $in, "<m-names.txt") or die "Could not open m-names.txt: $!\n";
while(<$in>) {
#next unless /(.*?)\/(.*)$/;
#next unless /(.*?)$/;
chomp;
$subnet = $_;
print "Checking $subnet\n";
@dnsoptions = `./getobjectlst.exe -u xx -p xx -a $subnet -o Rich`;
# 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 (not defined($dnsoptions[0])) {
print "dnsoptions is null or undefined, going on to the next subnet\n";
next;

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

# call "getsubnetlst.exe", passing $subnet as one of the parameters
@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
$snetpart2 = (split/"\s+"/, $line)[1];
# and now pass it to "getobjectlst.exe"; assign the output to
# the array @dnsoptions2
@dnsoptions2 = `./getobjectlst.exe -u xx -p xx -a $snetpart2 -o Rich`;
# find out counts of each subnettype (4th field from left)
foreach my $line (@dnsoptions2) {
@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} or 0), ($counts{DHCP} or 0), ($counts{Unused} or 0);
%counts = ();
}
}
close($in);
close($log);

Code
 
This is the error I get:

I still get that dang error ( but looks a little different)

Checking 10.0.0.0/8
Checking 146.149.0.0/16
Error 48: This subnet does not exist.
146.149.0.0/16
getobjectlst.exe :146.149.0.0/16: Error code = 48
dnsoptions is null or undefined, going on to the next subnet
Checking 161.16.0.0/16
Error 48: This subnet does not exist.
161.16.0.0/16
getobjectlst.exe :161.16.0.0/16: Error code = 48
dnsoptions is null or undefined, going on to the next subnet
Checking 161.56.0.0/16

When I run it manually it works just fine:

./getsubnetlst.exe -u xx -p xx -a 146.149.0.0/16 -t network -o Rich

"East" "146.149.89.0" "Y" "" "146.149.0.0" " " " " "255.255.255.224"
"West" "146.149.89.64" "Y" "" "146.149.0.0" " " " " "255.255.255.224"
"" "146.149.90.0" "Y" "" "146.149.0.0" " " " " "255.255.255.0"
"" "146.149.91.0" "N" "" "146.149.0.0" " " " " "255.255.255.0"

This maybe a clue..

we need to retain the mask bit "/16" or what ever is set on the m-names.txt file when it calls getsubnetlst.exe

I think perhaps we have a sort issue or the way the error is formatted??

Some history on this code:
I have this a code that I got help with for another task. I since tried to modify it to work on this task.

I need someones expertise to modify it slightly and I am not sure where to start or yet fully understand the logic.

I am trying to get a script to read my m-names.txt which has lots of
167.0.0.0/8
168.0.0.0/8
192.0.0.0/8

Then feed it into a cli called getobjectlst.exe with a -a flag that puts the subnet address from m-names.txt

This produces a report that looks like:

"167.227.31.14" "brs2002_204" "Server" "Static" "dc.dk.sark.com"
"167.227.31.15" "brs2005_284" "Server" "Static" "dc2.dk.sark.com"
"167.227.31.16" "brs2003" "Server" "Static" "dc4.dk.sark.com"

I am intrested in the 4th field entry value and how many (count) they are per $subnet=

I get a report on how many types etc... works great,

But I need it to also do another task, when I run this script I could run into a scenerio like this:
Error 48: This subnet does not exist.
146.149.0.0
getobjectlst.exe :146.149.0.0: Error code = 48

If I get an message like that, I need the script to engage in another routine;
If that subnet has that error 48" this subnet does not exist" then and run another cli called "getsubnetlst.exe" like so

./getsubnetlst.exe -u Xxx -p Xxx -a 146.149.0.0/16 -t netwrok -o rich

This will produce relatively the same output as in the first code:
"East" "146.149.1.0" "N" "" "146.149.0.0" " " " " "255.255.255.128"
"West-Subnet" "146.149.16.0" "Y" "" "146.149.0.0" " " " " "255.255.255.252"
"North Subnet" "146.149.16.128" "Y" "" "146.149.0.0" " " " " "255.255.255.240"
"Asia D-NAT" "146.149.22.0" "Y" "" "146.149.0.0" " Japan" " " "255.255.255.0"

Then I need to run the same cli getobjectlst.exe and reference the subnet address in the second whitespace(146.149.1.0) for each one that It reports.

Loop all through this output, then start again on the next line m-names.txt until all done


(This post was edited by richsark on May 1, 2009, 6:34 AM)


Edit Log:
Post edited by richsark (User) on May 1, 2009, 6:34 AM


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

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