http://qs1969.pair.com?node_id=179822


in reply to Re: using return to create a module object
in thread using return to create a module object

That shows that trying to retype code to simplify it shouldnt be done after 2am. This time I am pasting the code as it is.
#!/usr/bin/perl -w use strict; use Net::SNMP; my @devices = qw(2500 7200 7500 12000); my $retries = "1"; my $wait = "5.0"; my $mtu = "1500"; my $version = "1"; my $debug = "0"; my $community = "public"; my $snmp_username = ""; ## v3 my $snmp_authkey = ""; ## v3 my $snmp_authpassword = ""; ## v3 my $snmp_authprotocol = ""; ## v3 my %oid = ( version => ".1.3.6.1.2.1.1.1.0", ); &execute("run","tftp"); sub execute { ## pull source and destination of files my %args = ( s => $_[0], d => $_[1], ); foreach (@devices) { chomp $_; $args{h} = $_; &action(\%args); } } sub action { my $args = shift; ## set up initial parameters for this nodes snmp session(s) my $s = &create_snmp($args); ## grab the ios major revision number my $ios_version = $s->get_request ($oid{version}); ## grab an error if it exists $args->{e} = $s->error; ## close the snmp session $s->close; print "$args->{h} has IOS:\n\n$ios_version\n\n"; } sub create_snmp { ## get hostname to be used into local variable my $args = shift; ## create the hash of version independent options to pass to the met +hod my %snmp_options = ( hostname => "$args->{h}", retries => "$retries", timeout => "$wait", mtu => "$mtu", version => "$snmp_version", debug => "$debug", ); ## declare our variable to be set and returned my $s; ## test for snmpv3 usage if ( $snmp_version eq "snmpv3" ) { $snmp_options{username} = "$snmp_username"; $snmp_options{authkey} = "$snmp_authkey" if ($snmp_authkey); $snmp_options{authpassword} = "$snmp_authpassword" if ($snmp_authpassword); if ($snmp_authprotocol) { $snmp_options{authprotocol} = "$snmp_authprotocol"; $snmp_options{privkey} = "$snmp_authkey"; $snmp_options{privpassword} = "$snmp_authpassword"; } ## initiate session with v3 values $s = Net::SNMP->session(\%snmp_options); } else { ## set the v1/2c specific session traits $snmp_options{community} = $community; ## initiate session with v1/2c values $s= Net::SNMP->session(\%snmp_options); } ## provide our new session information return $s; ## end create_snmp() }

Thanks for looking into this. -c

Replies are listed 'Best First'.
Re: Re: Re: using return to create a module object
by dws (Chancellor) on Jul 06, 2002 at 17:44 UTC
    I don't see anything obviously wrong, though it looks odd to be using a device name as a host name. Is that really what you intend?

    You might gain some insight by trying to come up with a small example that demonstrates the problem. Something like

    use strict; use Net::SNMP; my $version = ".1.3.6.1.2.1.1.1.0"; my %snmp_options = ( ... }; $s = NET::SNMP->session(\%snmp_options); print $s->get_request($version); $s->close();
    and then explicitly fill in %snmp_options.

    This approach strips out extraneous detail, and should help you isolate the problem quickly.

(jeffa) 3Re: using return to create a module object
by jeffa (Bishop) on Jul 06, 2002 at 19:07 UTC
    You should get out of the habit of unnecessarily quoting variables - it is bad habit. Here are a couple of corrections:
    my %snmp_options = ( hostname => $args->{h}, retries => $retries, # etc ); # and $snmp_options{username} = $snmp_username;
    Sure, using quotes works for those examples, but it is unnecessary and will one day lead to something like:
    package My::CGI; use strict; use CGI; sub new { my $class = shift; my $foo = 'bar'; my $q = CGI->new(); my $self = { foo => "$foo", cgi => "$q", }; return bless $self, $class; } package main; use strict; my $q = My::CGI->new(); print $q->{foo}, "\n"; print $q->{cgi}->a({href=>'http://foo.bar'},'some link'),"\n"; __END__ yields: bar Can't locate object method "a" via package "CGI=HASH(0x80f8a20)" at ./foo.pl line 26.
    Whoops! What happened? You literalized a reference. Try this code out, then go back and remove the unnecessary quotes inside the $self hash ref.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)