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

 

First page Previous page 1 2 Next page Last page  View All


richsark
User

May 1, 2009, 6:33 AM

Post #1 of 30 (4913 views)
need another set of eyes on my perl script Can't Post

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)


FishMonger
Veteran / Moderator

May 1, 2009, 7:30 AM

Post #2 of 30 (4909 views)
Re: [richsark] need another set of eyes on my perl script [In reply to] Can't Post

Rather than duplicating everyone's effort, lets refer back to the source of this script.

http://www.unix.com/shell-programming-scripting/108484-count-script-wrapper-help-2.html

Don't run the command with backticks.

Options:
1) Use a system call and check the return code to verify that the command exited with an exit code of 0, which means it was successful.

2) use IPC::Open3 http://search.cpan.org/~nwclark/perl-5.8.9/lib/IPC/Open3.pm

3) use IPC::Run http://search.cpan.org/~adamk/IPC-Run-0.82/lib/IPC/Run.pm


richsark
User

May 1, 2009, 7:34 AM

Post #3 of 30 (4907 views)
Re: [FishMonger] need another set of eyes on my perl script [In reply to] Can't Post

HI Fishmonger, your right, I should of pasted the source of the request.

I thought by giving a history it was enough.

I will read through your links, I hope I can understand.


richsark
User

May 1, 2009, 8:12 AM

Post #4 of 30 (4903 views)
Re: [richsark] need another set of eyes on my perl script [In reply to] Can't Post

HI, I read through the links, but I still dont understand what I need to do.

Do I need to change where I have this:
open(my $in, "<m-names2.txt") or die "Could not open m-names.txt: $!\n";

To something like this?

my($wtr, $rdr, $err);
$pid = open3($wtr, $rdr, $err,
'some cmd and args', 'optarg', ...);


FishMonger
Veteran / Moderator

May 1, 2009, 8:19 AM

Post #5 of 30 (4902 views)
Re: [richsark] need another set of eyes on my perl script [In reply to] Can't Post

No, not m-names2.txt, you'd use open3 to run your commands that you're currently doing via the backticks.


(This post was edited by FishMonger on May 1, 2009, 8:20 AM)


richsark
User

May 1, 2009, 9:47 AM

Post #6 of 30 (4893 views)
Re: [FishMonger] need another set of eyes on my perl script [In reply to] Can't Post

Hi,
Ok i give up, if you have an idea could you give me an example referenceing my issue please?


FishMonger
Veteran / Moderator

May 1, 2009, 10:44 AM

Post #7 of 30 (4888 views)
Re: [richsark] need another set of eyes on my perl script [In reply to] Can't Post

I don't have the getobjectlst.exe porgram that you're using in that script, so before I work up an example using some other command, see if this adjustment to Tyler's script does what you need.

Change:

Code
if (@dnsoptions) { # @dnsoptions is not empty


To:

Code
if (@dnsoptions and $dnsoptions[0] ne '') { # @dnsoptions is not empty


In cases like this it's very helpful to use Data::Dumper to inspect the contents of vars.

http://search.cpan.org/~ilyam/Data-Dumper-2.121/Dumper.pm

So, in your case you could do:

Code
@dnsoptions = `./getobjectlst.exe -u Xxx-p Xxx -a $subnet -o rich`; 
print Dumper \@dnsoptions

What does it show for the value of $VAR1

You can also run this test script, which may give a better idea of what I'm talking about.

Code
#!/usr/bin/perl 

use strict;
use warnings;
use Data::Dumper;

my @array;

# run the script then uncomment this next line and see the difference
#$array[0] = '';

if( @array ) {
print "yes\n";
}
else {
print "no\n";
}

print Dumper \@array;



(This post was edited by FishMonger on May 1, 2009, 10:45 AM)


richsark
User

May 1, 2009, 11:05 AM

Post #8 of 30 (4884 views)
Re: [FishMonger] need another set of eyes on my perl script [In reply to] Can't Post

Thanks FishMonger,

So looking at Tylers script:

#!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
 
Where do I replace what you indicated to test?
Change:
Code

if (@dnsoptions) { # @dnsoptions is not empty


To:
Code

if (@dnsoptions and $dnsoptions[0] ne '') { # @dnsoptions is not empty

I dont see that?


FishMonger
Veteran / Moderator

May 1, 2009, 11:17 AM

Post #9 of 30 (4881 views)
Re: [richsark] need another set of eyes on my perl script [In reply to] Can't Post

This is the version of his script I was referring to.

Code
#!perl -w 
my @dnsoptions;
my $subnet;
open(my $in, "<m-names2.txt") or die "Could not open m-names2.txt: $!\n";
while(<$in>) {
$subnet = $_;
print "Checking $subnet\n";
@dnsoptions = `./getobjectlst.exe -u Xxx-p Xxx -a $subnet -o rich`;
if (@dnsoptions) { # @dnsoptions is not empty
if (join(" ",@dnsoptions) =~ /Error 48: This subnet does not exist./) {
print "dnsoptions contains the Error 48.\n";
} else {
print "dnsoptions was created successfully, it's size = $#dnsoptions\n";
}
} else {
print "dnsoptions is null or undefined, going on to the next subnet\n";
}
print "====================================\n";
}
close($in);


BTW, please use the code tags, it makes it much easier to read.


(This post was edited by FishMonger on May 1, 2009, 11:19 AM)


richsark
User

May 1, 2009, 12:13 PM

Post #10 of 30 (4873 views)
Re: [FishMonger] need another set of eyes on my perl script [In reply to] Can't Post

HI, using just this:

Code
#!perl -w  
my @dnsoptions;
my $subnet;
open(my $in, "<m-names2.txt") or die "Could not open m-names2.txt: $!\n";
while(<$in>) {
$subnet = $_;
print "Checking $subnet\n";
@dnsoptions = `./getobjectlst.exe -u Xxx-p Xxx -a $subnet -o rich`;
if (@dnsoptions) { # @dnsoptions is not empty
if (join(" ",@dnsoptions) =~ /Error 48: This subnet does not exist./) {
print "dnsoptions contains the Error 48.\n";
} else {
print "dnsoptions was created successfully, it's size = $#dnsoptions\n";
}
} else {
print "dnsoptions is null or undefined, going on to the next subnet\n";
}
print "====================================\n";
}
close($in);

Code
I got 

$ perl fish2.pl
Checking 10.0.0.0/8

dnsoptions was created successfully, it's size = 253
====================================
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

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

Error 48: This subnet does not exist.
162.2.0.0/16
getobjectlst.exe :162.2.0.0/16: Error code = 48
dnsoptions is null or undefined, going on to the next subnet
====================================
Checking 162.5.0.0/16


This is making me crazy, what can I do ??


FishMonger
Veteran / Moderator

May 1, 2009, 12:26 PM

Post #11 of 30 (4870 views)
Re: [richsark] need another set of eyes on my perl script [In reply to] Can't Post

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)


FishMonger
Veteran / Moderator

May 1, 2009, 12:44 PM

Post #12 of 30 (4865 views)
Re: [FishMonger] need another set of eyes on my perl script [In reply to] Can't Post

I forgot 1 set of debug print statements.

Here's the updated version.


Code
#!/usr/bin/perl 

usw strict;
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`;

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


# 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:45 PM)


richsark
User

May 1, 2009, 5:27 PM

Post #13 of 30 (4854 views)
Re: [FishMonger] need another set of eyes on my perl script [In reply to] Can't Post

Hi Fish,

I tried your code, but I got some errors:

$ perl fish3.pl
Global symbol "@subnettype" requires explicit package name at fish3.pl line 84.
Global symbol "@subnettype" requires explicit package name at fish3.pl line 85.
Execution of fish3.pl aborted due to compilation errors.


FishMonger
Veteran / Moderator

May 1, 2009, 5:53 PM

Post #14 of 30 (4852 views)
Re: [richsark] need another set of eyes on my perl script [In reply to] Can't Post

Sorry, I had a typo on line 3 and forgot the my keyword when declaring that var.

Change line 3 from:
usw strict;

to:
use strict;

Change line 84 from:
@subnettype = split/"\s+"/, $line;

to:
my @subnettype = split/"\s+"/, $line;


richsark
User

May 4, 2009, 4:45 AM

Post #15 of 30 (4823 views)
Re: [FishMonger] need another set of eyes on my perl script [In reply to] Can't Post

Hi FishMonger,

I made the changes, still have a minor issue:

BTW... I did see the spelling mistake in use strict; but I was not that luckly..

Anyway

$ perl fish3.pl
Checking 10.0.0.0/8
Use of uninitialized value $subnettype[3] in hash element at fish3.pl line 91, <$in> line 1.
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
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


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


FishMonger
Veteran / Moderator

May 4, 2009, 9:17 AM

Post #16 of 30 (4813 views)
Re: [richsark] need another set of eyes on my perl script [In reply to] Can't Post


Quote
Use of uninitialized value $subnettype[3] in hash element at fish3.pl line 91, <$in> line 1.


That means that array element doesn't have a value or you could say that its value is undef, which can not be assigned as a hash key.

The error message is referring to this line:
$counts{$subnettype[3]}++;

In the code I posted, that would be line 76 or 85, but the error message says line 91. What modifications to the script did you make?

What's in line 1 of your file and how does it differ from the other lines?


(This post was edited by FishMonger on May 4, 2009, 9:21 AM)


richsark
User

May 4, 2009, 10:44 AM

Post #17 of 30 (4805 views)
Re: [FishMonger] need another set of eyes on my perl script [In reply to] Can't Post

HI,

I am using the code you indicated.
Line 1 is my perl line

What is the issue?


Code
#!perl 
use strict;
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 x-p x-o Rich -a $subnet`;

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 -o Rich -a $subnet -t network`;

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


# 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 -o Rich -a $snetpart2`;

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) {
my @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);


Code



      
    


FishMonger
Veteran / Moderator

May 4, 2009, 10:55 AM

Post #18 of 30 (4803 views)
Re: [richsark] need another set of eyes on my perl script [In reply to] Can't Post

I was referring to line 1 of your m-names.txt file.

Line 91 of the script is close($log); which would not generate the warning message you received. So, you must have been testing a different version of the script.

Have you looked at the dump.log file?


FishMonger
Veteran / Moderator

May 4, 2009, 10:57 AM

Post #19 of 30 (4801 views)
Re: [richsark] need another set of eyes on my perl script [In reply to] Can't Post

Change:
if ( @dnsoptions and $dnsoptions[0] = '' ) {

to:
if ( @dnsoptions and $dnsoptions[0] eq '' ) {


richsark
User

May 4, 2009, 11:02 AM

Post #20 of 30 (4799 views)
Re: [FishMonger] need another set of eyes on my perl script [In reply to] Can't Post

HI,

m-names.txt looks fine, just as it was before:

10.0.0.0/8
146.149.0.0/16
161.16.0.0/16
161.56.0.0/16
162.2.0.0/16
162.5.0.0/16
162.15.0.0/16

The dump.log shows

dumping @dnsoptions
$VAR1 = '"10.0.0.1" "" "" "Unused" ""
';
$VAR2 = '"10.0.0.2" "" "" "Unused" ""
';
$VAR3 = '"10.0.0.3" "sarkie-04crp-w04" "Switch" "Static" "netdevice.sark.com"
';
$VAR4 = '"10.0.0.4" "" "" "Unused" ""
';
$VAR5 = '"10.0.0.5" "" "" "Unused" ""
';
$VAR6 = '"10.0.0.6" "rich-ntp-server" "Server" "Static" "netdevice2.sark.com"
';
$VAR254 = '"10.0.0.254" "" "" "Unused" ""
=========================
dumping @dnsoptions
=========================
dumping @dnsoptions
=========================
dumping @dnsoptions
=========================
dumping @dnsoptions
=========================
dumping @dnsoptions
$VAR1 = '"162.5.0.1" "" "" "Unused" ""
';
$VAR2 = '"162.5.0.2" "" "" "Unused" ""
';
$VAR3 = '"162.5.0.3" "" "" "Unused" ""


What do you think it is?


richsark
User

May 4, 2009, 11:18 AM

Post #21 of 30 (4797 views)
Re: [richsark] need another set of eyes on my perl script [In reply to] Can't Post

HI,

I made the change as you suggested. Seems like when there is an error 48, and then it runs the


Code
my @dnsoptions2 = `./getobjectlst.exe -u xx -p xx -o Rich -a $snetpart2`; 

Code
 

$ ./qip-getsubnetlst.exe -u xx -p xx -o Rich -a 146.149.0.0/16 -t network
"Nat-Bogota-Colombia" "146.149.1.0" "N" "" "146.149.0.0" " " " " "255.255.255.128"
"Apogee imported subnet" "146.149.16.0" "Y" "" "146.149.0.0" " " " " "255.255.255.252"

Its does not seem to run what the output from that, for example you can see from above these two subnets:

146.149.1.0
146.149.16.0

its suppose to then run
./getobjectlst.exe -u xx -p xx -o Rich -a 146.149.1.0

then 146.149.16.0 and so on all the way done that list.


(This post was edited by richsark on May 4, 2009, 11:18 AM)


richsark
User

May 4, 2009, 3:39 PM

Post #22 of 30 (4786 views)
Re: [richsark] need another set of eyes on my perl script [In reply to] Can't Post

HI,

anyone have an thoughts or ideas??

Feel free to jump in Cool


FishMonger
Veteran / Moderator

May 4, 2009, 4:01 PM

Post #23 of 30 (4785 views)
Re: [richsark] need another set of eyes on my perl script [In reply to] Can't Post

It's time for you to step up to the plate and try to do some troubleshooting/debugging, which you haven't done yet, rather than expecting someone else to provide you with a fully tested and debugged script that meets your specifications. If you need someone else to write this for you, then you should hire someone.


richsark
User

May 4, 2009, 4:07 PM

Post #24 of 30 (4783 views)
Re: [FishMonger] need another set of eyes on my perl script [In reply to] Can't Post

Hi FishMonger,

Belive me sir/mam, I have tried on my own all day and every day, trying different options. I am not sitting back and waiting if thats what you mean.

I have posted here as a novice/beginner since I am not up to speed yet. You experts have tought me a thing or two, but I keep getting over my head and hard to learn such advanced coding.

Please understand, I am not looking for a easy meal ticket, just some guidence and help during these trying days.

Please understand. I think its simple to the fact that when the output gets the Error 48, the second and most important part is not being read some-how. The rest is working fine, its just that one part.

Thanks


(This post was edited by richsark on May 4, 2009, 4:08 PM)


richsark
User

May 4, 2009, 4:35 PM

Post #25 of 30 (4777 views)
Re: [richsark] need another set of eyes on my perl script [In reply to] Can't Post

Here is the debugging:

$ perl -d fish3.pl

Loading DB routines from perl5db.pl version 1.3
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(fish3.pl:6): open my $log, '>', 'log-external.txt' or die "Could not open log: $!";

I have this file created, I see it in the working directory

Here is the contents of the log-external.txt:

Subnet,Static,DHCP,Unused
10.0.0.0/8
,37,0,217
146.149.0.0/16
,0,0,0
161.16.0.0/16
,0,0,0
161.56.0.0/16
,0,0,0
162.2.0.0/16
,0,0,0
162.5.0.0/16
,0,0,1022
162.15.0.0/16
,0,0,0
162.30.0.0/16
,0,0,0
162.34.0.0/16
,0,0,0
165.2.0.0/16
,0,0,254
165.4.0.0/16
,0,0,0

First page Previous page 1 2 Next page Last page  View All
 
 


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

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