I was wondering if I could get some help parsing the results from an SNMPWALK call. I know that there a few questions on here associated with almost this exact question. For example I looked at and tried the suggested solutions from these two questions:
To begin here is the SNMPWALK command and result I am trying to parse:
SNMPWALK Command:
snmpwalk -v1 -c public 192.168.2.51 -Ovq IF-MIB::ifDescr
SNMPWALK Result:
Software Loopback Interface 1.
WAN Miniport (SSTP).
WAN Miniport (L2TP).
WAN Miniport (PPTP).
WAN Miniport (PPPOE).
WAN Miniport (IPv6).
WAN Miniport (Network Monitor).
WAN Miniport (IP).
RAS Async Adapter.
Atheros AR8152 PCI-E Fast Ethernet Controller.
Realtek RTL8191SE Wireless LAN 802.11n PCI-E NIC.
...
Basically what I am trying to do is search for "Wireless LAN 802.11(?) PCI-E Nic."; where the ? represents values a-z, and strips away any excess value after the NIC.
So essentially from the list above the only value that would be returned is the Realtek RTL8191SE Wireless LAN 802.11n PCI-E NIC.
with the Realtek RTL8191SE
portion removed. I would also like for the solution to not return items that have values after the NIC.
. For example if give something like this:
Realtek RTL8191SE Wireless LAN 802.11n PCI-E NIC - Deterministic Network Enhancer Miniport-VirtualBox NDIS Light-Weight Filter-0000.
the solution should reject it based on the additional values at the end.
Here is what my code currently looks like:
#!/bin/bash
...
IFS=$'\n'
var=($(snmpwalk -v1 -c public -Ovq $1 IF-MIB::ifDescr))
for i in "${var[@]}"; do
p=$(echo "$i" | sed 's/^.*\(Wireless LAN 802.11n PCI-E NCI.*\)/\1/')
# if [[ "$p" == "Wireless LAN 802.11n PCI-E NCI." ]]; then
echo "$p"
# fi
done
...
What I have realized from playing around with setting the SNMPWALK command output to an array is that each item gets added in as space separated values. Thus I set IFS to newline delimiter first. Then I tried to match each line based on what I said above.