Re: adding elements to an array (120 Views)
Reply
Occasional Visitor
Chris Vidal
Posts: 2
Registered: ‎07-13-2006
Message 1 of 5 (120 Views)

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
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 2 of 5 (120 Views)

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}'
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 3 of 5 (120 Views)

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.
Honored Contributor
Ralph Grothe
Posts: 2,631
Registered: ‎04-09-2000
Message 4 of 5 (120 Views)

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
Occasional Visitor
Chris Vidal
Posts: 2
Registered: ‎07-13-2006
Message 5 of 5 (120 Views)

Re: adding elements to an array

Done
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.