Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

SNMP - cont

by theroninwins (Friar)
on Aug 27, 2004 at 13:09 UTC ( #386350=perlquestion: print w/replies, xml ) Need Help??

theroninwins has asked for the wisdom of the Perl Monks concerning the following question:

ok this is the prog til now:

#!/usr/bin/perl -w use Net::SNMP; use strict; use warnings; use diagnostics; open IPFILE, "ipfile.txt" or die "Can't get IPs - $!\n"; my $community = 'public'; my $ifIndex = '1.3.6.1.2.1.47.1.1.1.1.6'; #1.3.6.1.2.1.2.2.1.1 + 1.3.6.1.2.1.47.1.1.1.1.12 my $ifDescr = '1.3.6.1.2.1.47.1.1.1.1.11'; #1.3.6.1.2.1.47.1.1.1.1. +13 my $ifDescr2 = '1.3.6.1.2.1.47.1.1.1.1.2'; #1.3.6.1.2.1.47.1.1.1.1. +13 my @test1; my @test2; while ( my $ip = <IPFILE> ) { chomp $ip; print "Got: $ip\n"; my ( $session, $error ) = Net::SNMP->session( -hostname => $ip, -community => $community, -port => 161 ); my $response; if ( defined( $response = $session->get_table($ifIndex) ) ) { foreach my $index ( values %{$response} ) #values { my $this_desc = "$ifDescr.$index"; my $description; if ( defined( $description = $session->get_request($this_d +esc) ) ) { my @serial = values %{$description}; #open OUT, ">>serlist.txt"; #print OUT @serial; #close OUT; #open (OUT, ">>serlist.txt"); #print keys %{$description}; #print '.......'; #print values %{$description}, "\n"; my @test1 = @serial; } } foreach my $index ( values %{$response} ) #values { my $this_desc = "$ifDescr2.$index"; my $description2; if ( defined( $description2 = $session->get_request($this_ +desc) ) ) { my @desc = values %{$description2}; #open OUT, ">>desclist.txt"; #print OUT @desc; #close OUT; #open (FILE, ">>desclist.txt"); #print keys %{$description}; #print '.......'; #print values %{$description2}, "\n"; } print my @test1; } } $session->close(); }
the @test1 should print the @serial stuff but it is not working maybe because i cannot get the stuff to be global?? Anyone an idea on how to do it??

Edited by Chady -- code tags addition.

Replies are listed 'Best First'.
Re: SNMP - cont
by davido (Cardinal) on Aug 27, 2004 at 13:56 UTC

    You've got a lot of scoping problems. The clues others have provided are definately on target, but the problem is that you've got more than one problem. Don't discount their remarks until you've hunted down each and every time that you're declaring the same variable multiple times. You can't just pepper a script with 'my' until strict stops complaining. You have to pay attention to the scope in which variables are declared, and to whether or not one declaration is masking another at broader scope.

    First, if you are declaring @test1 at a fairly broad scope to be accessible several places thoughout the script, then when you re-declare it at narrower scope and assign to it, that declaration masks the previous. You are ok assigning to it in that narrower scope, but the effects of the assignment vanish when the narrower scope ends. The culprit is this line: my @test1 = @serial;. If anything, it should be @test1=@serial;, so that the assignment is to the outter @test1, not the inner @test1 (these are two different variables, since you declared them twice at different scopes).

    Later, in an entirely different lexical block you're saying, "print my @test1;". How many times are you going to declare @test1? Because this is the third time, and consequently, a third @test1 (completely unrelated to the other two). It lives in its own lexical scope, and that masks any other @test1's declared at broader lexical scope.

    Ok, those are the most glaring scoping issues. But they're biggies. Don't just throw 'my' around like that. Declare a variable in the narrowest scope you can get away with, so long as that scope permits the variable to be accessible to all the code that needs it. You're declaring it once in that nice broad scope, but then two more times in narrower scope. The outter @test1 is never receiving a value, and never printing a value. The first inner @test1 is reeiving a value but then it falls out of scope and is lost forever. The secind inner @test1 is being printed, but it never received a value. That's what 'my' is doing for you.

    Next, don't place calls to the operating system without checking to see that they worked. That means, you should always check for failure in your opens. And if the filehandle is opened for output, check for success in your closes too. See perlopentut for details on that. Even your initialization of Net::SNMP->session() should have its return value checked for success. Otherwise, you'll just never be sure at what point the script is failing.


    Dave

      Thanks for that helüp, I now understand better what it is all about... I changed the code to the following:

      #!/usr/bin/perl -w use Net::SNMP; use strict; use warnings; use diagnostics; open IPFILE, "ipfile.txt" or die "Can't get IPs - $!\n"; my $community = 'public'; my $ifIndex = '1.3.6.1.2.1.47.1.1.1.1.6'; #1.3.6.1.2.1.2.2.1.1 + 1.3.6.1.2.1.47.1.1.1.1.12 my $ifDescr = '1.3.6.1.2.1.47.1.1.1.1.11'; #1.3.6.1.2.1.47.1.1.1.1. +13 my $ifDescr2 = '1.3.6.1.2.1.47.1.1.1.1.2'; #1.3.6.1.2.1.47.1.1.1.1. +13 my @serial; my @test2; while ( my $ip = <IPFILE> ) { chomp $ip; print "Got: $ip\n"; + my ( $session, $error ) = Net::SNMP->session( -hostname => $ip, -community => $community, -port => 161 ); my $response; if ( defined( $response = $session->get_table($ifIndex) ) ) { foreach my $index ( values %{$response} ) #values { my $this_desc = "$ifDescr.$index"; my $description; if ( defined( $description = $session->get_request($this_d +esc) ) ) { @serial = values %{$description}; #print @serial; } } foreach my $index ( values %{$response} ) #values { my $this_desc = "$ifDescr2.$index"; my $description2; if ( defined( $description2 = $session->get_request($this_ +desc) ) ) { my @desc = values %{$description2}; #open OUT, ">>desclist.txt"; #print OUT @desc; #close OUT; #open (FILE, ">>desclist.txt"); #print keys %{$description}; #print '.......'; #print values %{$description2}, "\n"; } } print @serial; } $session->close(); }


      however the problem is still the same for some reason I really don't get it yet. From my understanding it should be ok when i declare a Var once with my and then i can use it as a global var when i use the same var without the my infront of it . So this should be weorking. When i print the @serial within the same part i assign a value to it it does but like this is doesn't.
        Ok I got it going somehow, now it uses temp files but it works :-) Here it is my first prog :-).

        #!/usr/bin/perl -w use Net::SNMP; #use strict; use warnings; use diagnostics; open IPFILE, "ipfile.txt" or die "Can't get IPs - $!\n"; my $community = 'public'; my $ifIndex = '1.3.6.1.2.1.47.1.1.1.1.6'; #1.3.6.1.2.1.2.2.1.1 + 1.3.6.1.2.1.47.1.1.1.1.12 my $ifDescr = '1.3.6.1.2.1.47.1.1.1.1.11'; #1.3.6.1.2.1.47.1.1.1.1. +13 my $ifDescr2 = '1.3.6.1.2.1.47.1.1.1.1.2'; #1.3.6.1.2.1.47.1.1.1.1. +13 while ( my $ip = <IPFILE> ) { chomp $ip; print "Got: $ip\n"; + my ( $session, $error ) = Net::SNMP->session( -hostname => $ip, -community => $community, -port => 161 ); my $response; if ( defined( $response = $session->get_table($ifIndex) ) ) { foreach my $index ( values %{$response} ) #values { my $this_desc = "$ifDescr.$index"; my $description; if ( defined( $description = $session->get_request($this_d +esc) ) ) { my @serial = values %{$description}; open OUTPUT1, ">>serlist.txt"; print OUTPUT1 @serial, "\n"; close OUTPUT1; } } foreach my $index ( values %{$response} ) #values { my $this_desc = "$ifDescr2.$index"; my $description2; if ( defined( $description2 = $session->get_request($this_ +desc) ) ) { my @desc = values %{$description2}; open OUTPUT2, ">>desclist.txt"; print OUTPUT2 @desc, "\n"; close OUTPUT2; } } open(OUT, "serlist.txt"); open(OUT2, "desclist.txt"); my @outlist; while (<OUT>) { my %hash; my @temp = split(/;/,$_); $hash{'file1'} = $temp[0]; $hash{'file2'} = <OUT2>; #Return entfernen foreach (values %hash) { $_ =~ s/\n//g; } push(@outlist,\%hash); } close(OUT); open FINOUT, ">>finallist.txt"; print FINOUT "$ip\n"; foreach my $hashref (@outlist) { print FINOUT "$hashref->{'file1'};$hashref->{'file2'}\n"; } print FINOUT "\n"; close FINOUT; } $session->close(); }


        ... maybe you have an idea to do it without those temps.
Re: SNMP - cont
by Joost (Canon) on Aug 27, 2004 at 13:17 UTC
      when i change the print my @test1 to print @test1 it doesn't chnage it still does not print the result... I tried that already :-( .
        Doesn't change that print my @test is wrong
Re: SNMP - cont
by Eimi Metamorphoumai (Deacon) on Aug 27, 2004 at 13:37 UTC
    The real problem is that you're redefining @test1 instead of using the existing one. That is, you're doing my @test1 = @serial;, which creates a new variable named @test1, scoped to the current block, instead of assigning to the @test1 you defined earlier. If you replace it with just @test1 = @serial; (no my) you should be able to get to it later.
      thanks for the idea but it doesn't work. :-( .

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://386350]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2022-05-29 12:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (101 votes). Check out past polls.

    Notices?