I am working on monitoring a Cisco device where I would need to parse the snmp output and print it in separate more verbose lines.
Through the command line I can do this:
snmpwalk -m /usr/share/snmp/mibs/CISCO-UNIFIED-COMPUTING-STORAGE-MIB.my -v2c -c opennms-Priv X.X.X.X .1.3.6.1.4.1.9.9.719.1.45.4.1.18 2>/dev/null | awk '{print $4}' | while read -r line ; do echo "Drive $line" ; done
Drive online(1)
Drive online(1)
Drive online(1)
Drive online(1)
Drive online(1)
Drive online(1)
Drive online(1)
Drive online(1)
Drive online(1)
Drive online(1)
Drive online(1)
Drive online(1)
Drive online(1)
Drive online(1)
Drive online(1)
Drive online(1)
However when I use the same in a script:
#!/bin/bash
Output=`snmpwalk -m /usr/share/snmp/mibs/CISCO-UNIFIED-COMPUTING-STORAGE-MIB.my -v2c -c opennms-Priv 10.201.1.131 .1.3.6.1.4.1.9.9.719.1.45.4.1.18 2>/dev/null | awk '{print $4}'`
while read -r line ; do
echo "Drive $line";
done << $Output
I get this,
bash -x CIMC-Monitong.sh
++ snmpwalk -m /usr/share/snmp/mibs/CISCO-UNIFIED-COMPUTING-STORAGE-MIB.my -v2c -c opennms-Priv X.X.X.X .1.3.6.1.4.1.9.9.719.1.45.4.1.18
++ awk '{print $4}'
+ Output='online(1)
online(1)
online(1)
online(1)
online(1)
online(1)
online(1)
online(1)
online(1)
online(1)
online(1)
online(1)
online(1)
online(1)
online(1)
online(1)'
CIMC-Monitong.sh: line 9: warning: here-document at line 7 delimited by end-of-file (wanted `$Output')
+ read -r line
+ echo 'Drive '
Drive
+ read -r line
+ echo 'Drive '
Drive
+ read -r line
What am I missing?
Edit: The reason I didn't pipe it is, right now I am getting output similar to this:
Drive online(1) Drive online(1) Drive online(1) Drive online(1) Drive online(1) Drive online(1) Drive online(1) Drive online(1) Drive online(1) Drive online(1) Drive online(1) Drive online(1) Drive online(1) Drive online(1) Drive online(1) Drive online(1)
But I expect something like this:
Drive1 online(1) Drive2 online(1) Drive3 online(1) Drive4 online(1) Drive5 online(1) Drive6 online(1) Drive7 online(1) Drive8 online(1) Drive9 online(1) Drive10 online(1) Drive11 online(1) Drive12 online(1) Drive13 online(1) Drive14 online(1) Drive15 online(1) Drive16 online(1)