Operating System - Linux
1752805 Members
5411 Online
108789 Solutions
New Discussion юеВ

Re: adding elements to an array

 
Chris Vidal
New Member

adding elements to an array

snmpwalk -c xx yy system generates the following.

cvidal@cayd04-mds1:~> snmpwalk -c tmedomain4 192.168.63.247 system
sysDescr.0 = STRING: Cisco IOS Software, 3800 Software (C3845-ADVIPSERVICESK9-M), Version 12.4(3b), RELEASE SOFTWARE (fc3)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2005 by Cisco Systems, Inc.
Compiled Fri 09-Dec-05 09:08 by alnguyen
sysObjectID.0 = OID: enterprises.9.1.544
sysUpTime.0 = Timeticks: (28557767) 3 days, 7:19:37.67
sysContact.0 = STRING: MCI 800-256-9284
sysName.0 = STRING: merv-m008napa-687103
sysLocation.0 = STRING:
sysServices.0 = INTEGER: 78
sysORLastChange.0 = Timeticks: (0) 0:00:00.00

I would like to put each line into an array element as I receive the response for later printing or assigning to vars

set -A ARRAY just setsup a single element array.

tks
4 REPLIES 4
Sandman!
Honored Contributor

Re: adding elements to an array

>I would like to put each line into an array element as I receive the response for >later printing or assigning to vars

# snmpwalk -c xx yy system | awk '{l[NR]=$0}'
A. Clay Stephenson
Acclaimed Contributor

Re: adding elements to an array

Here's a simple example using the output of ls -l /tmp but it should give you the idea:

#!/usr/bin/sh

typeset PROG=${0}
typeset -i MAXARRAY=1023
typeset -i KNT=0
typeset X=""
typeset -i STAT=0

#your snmpwalk will replace the ls -l

ls -l /tmp | while read X
do
typeset A[${KNT}]="${X}"
((KNT += 1))
if [[ ${KNT} -gt ${MAXARRAY} ]]
then
STAT=255
echo "${PROG}: Missing elements" >&2
break
fi
done
# Now let's read 'em out
typeset -i I=0
while [[ ${I} -lt ${KNT} ]]
do
echo "${I} -> \"${A[${I}]}\""
((I += 1))
done
exit ${STAT}

----------------------------

Note that shell arrays are limited to a maximum of 1024 elements so you might want to rethink your problem (Perl?).
If it ain't broke, I can fix that.
Ralph Grothe
Honored Contributor

Re: adding elements to an array

You could maybe try something like

typeset -i i=-1; set -A SNMPWALK
/your/snmpwalk_cmd 2>/dev/null \
| while read line; do
SNMPWALK[$((i+=1))]="$line"
done

But beware not to start your snmpwalk too high in the tree because afaik a shell array can only hold up to 1024 elements.
Besides, because of wasteful memory usage
dumping large amounts of output in arrays is not considered terribly efficient programming.

In Perl it is much easier to store each output line as an array element,
and there you are only restricted by your process'es memory share.
But for efficiency's sake one shouldn't store it in an array.

e.g.

my @SNMPWALK = qx(/your/snmpwalk_cmd);

or if you want more control

local *SNMP_PIPE
my $child = open SNMP_PIPE, '-|';
die "Cannot open pipe to snmpwalk" if !defined $child;
if ($child) {
@SNMPWALK = ;
close SNMP_PIPE;
} else {
local %ENV; # if taint mode demands
exec qw(/path/to/snmpwalk arg1 arg2 arg3);
}
# maybe you want to chomp new lines
chomp @SNMPWALK
Madness, thy name is system administration
Chris Vidal
New Member

Re: adding elements to an array

Done