Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
There's more than one way to do things
 
PerlMonks  

Parsing SNMP VLAN-numbers

by Dave82764 (Initiate)
on May 16, 2006 at 01:32 UTC ( [id://549669]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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

Hi, I am fairly new to Perl so would appreciate if someone could point me in the right direction on this one.
I am using Perl to make SNMP gets on a network switch. I retrieve the data and push it onto an array. I now need however to extract just a portion of each string and put that into another array. In the example below, I wish to take the VLAN number (1,2,10,1002,1003) and put into a separate array. I have looked at split but am not sure if this can achieve what I am after. Any assistance is much appreciated.

.1.3.6.1.4.1.9.9.46.1.3.1.1.2.1.1:INTEGER:1
.1.3.6.1.4.1.9.9.46.1.3.1.1.2.1.2:INTEGER:1
.1.3.6.1.4.1.9.9.46.1.3.1.1.2.1.10:INTEGER:1
.1.3.6.1.4.1.9.9.46.1.3.1.1.2.1.1002:INTEGER:1
.1.3.6.1.4.1.9.9.46.1.3.1.1.2.1.1003:INTEGER:1
Thanks,

Dave

2006-05-16 Retitled by Corion, as per Monastery guidelines
Original title: 'Parsing'

Replies are listed 'Best First'.
Re: Parsing SNMP VLAN-numbers
by Zaxo (Archbishop) on May 16, 2006 at 01:52 UTC

    You can either split on dot or colon and index to the element you want, or else you can do regex matching and capture the field you want. In this case I think regex, since there is a unique pair of delimiters around the number you want,

    my @array = qw/ .1.3.6.1.4.1.9.9.46.1.3.1.1.2.1.1:INTEGER:1 .1.3.6.1.4.1.9.9.46.1.3.1.1.2.1.2:INTEGER:1 .1.3.6.1.4.1.9.9.46.1.3.1.1.2.1.10:INTEGER:1 .1.3.6.1.4.1.9.9.46.1.3.1.1.2.1.1002:INTEGER:1 .1.3.6.1.4.1.9.9.46.1.3.1.1.2.1.1003:INTEGER:1 /; my @vlans = map {m/\.(\d+):/ ? $1 : undef} @array;
    The trinary op makes sure that the elements of @vlans correspond one-to-one with those of @array, even when no match is found.

    After Compline,
    Zaxo

Re: Parsing SNMP VLAN-numbers
by davido (Cardinal) on May 16, 2006 at 01:53 UTC

    I would probably do it this way:

    my @vlan_numbers; foreach ( @snmp_strings ) { if( /\.(\d+):\w/ ) { push @vlan_numbers, $1; } } print "$_\n" foreach @vlan_numbers;

    Dave

Re: Parsing SNMP VLAN-numbers
by turo (Friar) on May 16, 2006 at 02:46 UTC

    Another way ^_^>

    #!/usr/bin/perl $_ = qq( .1.3.6.1.4.1.9.9.46.1.3.1.1.2.1.1:INTEGER:1 .1.3.6.1.4.1.9.9.46.1.3.1.1.2.1.2:INTEGER:1 .1.3.6.1.4.1.9.9.46.1.3.1.1.2.1.10:INTEGER:1 .1.3.6.1.4.1.9.9.46.1.3.1.1.2.1.1002:INTEGER:1 .1.3.6.1.4.1.9.9.46.1.3.1.1.2.1.1003:INTEGER:1 ); push @vlans, $1 while ( m/\.(\d+):\w+/g ); print "@vlans\n";

    perl -Te 'print map { chr((ord)-((10,20,2,7)[$i++])) } split //,"turo"'
Re: Parsing SNMP VLAN-numbers
by polettix (Vicar) on May 16, 2006 at 04:57 UTC
    Being new to Perl, you may ignore that there is a whole archive of modules to to (nearly) all kind of stuff: it's http://www.cpan.org/ (you can even http://search.cpan.org/). You can also find interesting SNMP stuff there.

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: Parsing SNMP VLAN-numbers
by idle (Friar) on May 16, 2006 at 06:11 UTC
    If you using snmpget, you can specify an option -Oqv, in this case you'll get only value of oid(1,2,10,1002,1003).
    And of course take a look at the span, the simplest way to make a things.
Re: Parsing SNMP VLAN-numbers
by Dave82764 (Initiate) on May 16, 2006 at 08:08 UTC
    Thanks for all the replies, I really appreciate your help. Now I am going to get busy trying them all out.
    Thanks again,
    Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://549669]
Approved by davido
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.