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

snmpwalk in perl

by Nouhira (Initiate)
on May 17, 2019 at 06:48 UTC ( [id://11100145]=perlquestion: print w/replies, xml ) Need Help??

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

I need a snmpwalk script in Perl without using modules(get_request etc) by using command `snmpwalk -v2c -$community $ip $oid`

Replies are listed 'Best First'.
Re: snmpwalk in perl
by afoken (Chancellor) on May 17, 2019 at 07:04 UTC
    I need a snmpwalk script in Perl without using modules

    ... and I really, really need a mammoth to ride to work, right now, ASAP!

    Are you aware that Perlmonks is not a code writing service? Show some effort. Start writing the script, show us where you are stuck, and we will gladly help you. Or make it a job and post it at https://jobs.perl.org/.

    Here's a starter:

    #!/usr/bin/perl use strict; use warnings; #### your code here ####

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      yes, so sorry for that. Im still in learning phase in perl, i wanted to snmpwalk for 3 to 4 IP's using snmpwalk command and store the result in an array, After that i want to use a 'for' loop to iterate that array and print the output for one IP at a time. I hope u understood my query. I will staring writing the script for one IP. Please correct it
      #!/usr/bin/perl use strict; use warnings; my $ip = "199.223.232.10"; my $community = "public"; my $oid = "1.3.6.1.2.1.25.2.2.0"; my @arr = `snmpwalk _v2c _$community _$ip _$oid`; print "@arr";

        Hello again Nouhira,

        On your sample of code you are not using _ (underscores) why on your sample of code you use?

        Does this code compile?

        On my LinuxOS test box your code gives me this error:

        $ perl test.pl _public: Unknown Object Identifier (Sub-id not found: (top) -> _public +)

        Please test your code before posting.

        Hope this helps, Thanos

        Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: snmpwalk in perl
by thanos1983 (Parson) on May 17, 2019 at 08:32 UTC

    Hello Nouhira,

    Welcome to the monastery.

    I am a bit stuck here. You do not want to use a module but at the same time you have provided the solution to your self:

    `snmpwalk -v2c -$community $ip $oid`

    What is wrong with this piece of code? Are you wondering how to store the stdout? Is it not working for you is it failing? Unless if you expect something else that you are not explaining.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      yes, so sorry for that. Im still in learning phase in perl, i wanted to perform snmpwalk for 3 to 4 IP's using snmpwalk command and store the result in an array, After that i want to use a 'for' loop to iterate that array and print the output for one IP at a time. I hope u understood my query.

        Hello again Nouhira,

        On this case a simple foreach loop with all the IPs should do for your case.

        Sample of code:

        #!/usr/bin/perl use strict; use warnings; my $oid = '1.3.6.1.2.1.25.2.2.0'; my @ips = qw(127.0.0.1 127.0.0.1); my $community = 'public'; foreach my $ip (@ips) { chomp(my @array = `snmpwalk -v2c -c $community $ip $oid`); }

        As a next step you should find a way to store your data for each IP. On this case my approach would be HASHES OF ARRAYS but it can also be done by ARRAYS OF ARRAYS. I would highly recommend to read the documentation to understand the difference from one to the other and also how to add data to them and how to access the data after.

        Then Job done.

        If you have any problems after experimenting with the code then you can ask us again.

        Hope this helps, Thanos

        Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-19 04:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found