Very new to scripting, basically learning on the job. So, I have a task I'm trying to complete. What I need is comparing an input file and running the snmpwalk command to find out which 3 community strings a device is using.
this is what I have so far:
(I've replaced what the community string is with just community01/02/03)
while read line
do
device_ip=`echo $line | cut -f1 -d","`
device_name=`echo $line | cut -f2 -d","`
alarm_title=`echo $line | cut -f3 -d","`
if [[ $alarm_title == "MANAGEMENT AGENT LOST" ]]; then
snmpwalk01=`snmpwalk -v 2c -c community01 "$device_ip" sysName.0`
if [[ -z "$snmpwalk01" ]; then
snmpwalk02=`snmpwalk -v 2c -c community02 "$device_ip" sysName.0`
if [[ -z "$snmpwalk02" ]]; then
snmpwalk03=`snmpwalk -v 2c -c community03 "$device_ip" sysName.0`
if [[ -z "$snmpwalk03" ]]; then
echo "$device_name"="unknown"
fi
fi
fi
fi
I'm sure I've completely butchered everything...but this is what I've got by googling, looking at scripts already on the server, etc etc.
Like I said, I have an input file that has the Device IP, Device Name, and the Alarm Detail. Not all of the alarm details are "Management Agent Lost". What I'm trying to do is take one line from that input file, see if the community01 works as a community string. If it doesn't, then test community02. If that doesn't work, test 03.
If none work, echo the device name and the result.
If one of them works, echo device name and the community string that does work.
With quite a lot of assumptions on e.g. your input file's structure, the shell you run ( bash ?), and your output format, this might do what you want:
while IFS=, read IP NAME TITLE
do [ "$TITLE" = "MANAGEMENT AGENT LOST" ] || continue
FOUND=0
for i in 01 02 03
do smnpwalk[$i]=$(snmpwalk -v 2c -c community$i "$IP" sysName.0)
[ "${smnpwalk[$i]}" ] && { FOUND=1; break; }
done
[ $FOUND == 1 ] && echo "device $NAME : ${smnpwalk[$i]} community$i" || echo "device $NAME unknown"
done < file
It reads the variables immediately from the flle, no cut needed, then tests the title for the desired string ( [ ... ] && ... || ... is a shortcut if - then - else which may fail on some complex occasions), skips the line if title is wrong, or else for loops across the desired community extensions (01, 02, 03), fills in an array indexed by the extension, and, if sth. found, breaks out of the loop. Then a message is produced depending on sth. found or not. If the extensions are non-numeric, you'll need to declare an associative array first.
The problem I've been running into is that one guy that's teaching me is more into bash, and the other guy that's teaching me is more into ksh. So it's been getting a bit confusing.
I'd rather get into bash, only because I've been told it's more popular.
I'll look into the commands, and see if I can get it working.
Most of syntax between ksh93 and bash is same. All previous scripts works fine in bash and ksh93.
Both support Posix syntax.
Standard
test / [
comparing is single =, not ==, but bash and ksh93 accept also using C-like == comparing. Dash is pure Posix compatible and not accept == test comparing.
So standard Posix version is, which works in all Posix-shells (ksh, bash, dash, ...):
[ $FOUND = 1 ] && echo "device $NAME : ${smnpwalk[$i]} community$i" || echo "device $NAME unknown"
# or using conditional command [[ ]], but it's not in Posix standard.
RudiC has used more built-in syntax = faster and not create so much subprocessing.