zonevbkr has asked for the wisdom of the Perl Monks concerning the following question:
After getting great help before, I'm back for more. This time I'm working with a similar but different array. I need to do a few things here.... 1) I need to pull any / all lines that start with 'snmp-server host' and stick them into a variable and have this variable formatted as one long line of multiple lines - it'd look similar to this: snmp-server host 10.234.171.46 admin\nsnmp-server host 10.234.171.51 admin frame-relay config snmp\nsnmp-server host 10.234.171.75 admin\n 2) I need to substitute the 4th word in the line(s) starting with 'snmp-server' that is separated by spaces and make it 'Bob' and stick this in a variable, and format it the same as with #1 3) I need to substitute 'snmp-server' with 'no snmp-server' and stick this in a variable I can get #1 & #3 to work if there's just one occurrance in the array, but if there's more than one, then I just get the first one. Any suggestions for any parts would be awesome. thanks, Chris
So, the output from my code could look like: $VAR1 = [ 'snmp-server host 10.234.171.38 JCIPPUBL no cdp advertise-v2 no cdp run !' ]; server host 10.234.171.38 abcd no snmp-server host 10.234.171.38 abcd \n or there can be multiple lines such as this: $VAR1 = [ 'snmp-server host 10.234.171.45 admin snmp-server host 10.234.171.46 admin snmp-server host 10.234.171.51 admin frame-relay config snmp snmp-server host 10.234.171.75 admin snmp-server host 10.234.171.51 publ frame-relay config snmp snmp-server host 10.234.171.54 publ frame-relay config snmp snmp-server host 10.234.171.59 publ !snmp-server host 10.234.171.46 admin snmp-server host 10.234.171.51 admin frame-relay config snmp snmp-server host 10.234.171.75 admin snmp-server host 10.234.171.51 publ frame-relay config snmp snmp-server host 10.234.171.54 publ frame-relay config snmp snmp-server host 10.234.171.59 publ !snmp-server host 10.234.171.51 admin frame-relay config snmp snmp-server host 10.234.171.75 admin snmp-server host 10.234.171.51 publ frame-relay config snmp snmp-server host 10.234.171.54 publ frame-relay config snmp snmp-server host 10.234.171.59 publ !snmp-server host 10.234.171.75 admin snmp-server host 10.234.171.51 publ frame-relay config snmp snmp-server host 10.234.171.54 publ frame-relay config snmp snmp-server host 10.234.171.59 publ !snmp-server host 10.234.171.51 publ frame-relay config snmp snmp-server host 10.234.171.54 publ frame-relay config snmp snmp-server host 10.234.171.59 publ !snmp-server host 10.234.171.54 publ frame-relay config snmp snmp-server host 10.234.171.59 publ !snmp-server host 10.234.171.59 publ !' ]; server host 10.234.171.45 admin no snmp-server host 10.234.171.45 admin \n The code is currently: #!/usr/bin/perl use lib qw(blib lib); #use strict; use warnings; use Getopt::Long; use Pod::Usage; use Data::Dumper; use Opsware::NAS::Client; use Net::Ping; use POSIX qw(strftime); #use TrueControlAPI; my($user,$pass,$host) = ('na_admin', 'na4adm1n', 'localhost'); my $help = 0; my $nas = Opsware::NAS::Client->new(); my $res = $nas->login(-user => $user, -pass => $pass, -host => $host, +-nosession => '0'); print " \n"; #$res = $nas->show_configlet(host => "dev1", start => "snmp-server hos +t", end => "!"); $res = $nas->show_configlet(host => "dev2", start => "snmp-server host +", end => "!"); #$res = $nas->show_deviceinfo(ip => '10.253.74.70'); unless ($res->ok()) { printf STDERR ("*** error: %s\n", $res->error_message()); exit(1); } print (Dumper([$res->result()])); foreach my $var1 ($res->result()) { #my ($image) = $var1 =~ /^SystemImage:\s*([^\n]+)/m my ($snmp) = $var1 =~ /^snmp-\s*([^\n]+)/m or die("No SystemImage\n"); print("$snmp\n"); my ($no_snmp) = 'no snmp-' . $snmp . '\n'; print("$no_snmp\n"); } $nas->logout();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: pulling more data from an array
by kennethk (Abbot) on Aug 04, 2010 at 20:36 UTC | |
by zonevbkr (Initiate) on Aug 05, 2010 at 01:49 UTC | |
by kennethk (Abbot) on Aug 05, 2010 at 14:36 UTC | |
by zonevbkr (Initiate) on Aug 05, 2010 at 19:26 UTC | |
by kennethk (Abbot) on Aug 05, 2010 at 20:17 UTC | |
| |
|
Re: pulling more data from an array
by aquarium (Curate) on Aug 05, 2010 at 03:21 UTC |