Kareem Hamed
Kareem Hamed

Reputation: 21

Parse SNMP output in Bash

I need to process the text output from the below command:

snmpwalk -v2c -c community 192.168.122.15 .1.3.6.1.4.1.14823.2.2.1.4.1.2.1.39

The Original output is:

SNMPv2-SMI::enterprises.14823.2.2.1.4.1.2.1.39.252.200.151.233.54.69.197.39.5.77 = STRING: "Android"

I need the output to look like

197.39.5.77="Android"

197.39.5.77 is the last four digits before the = sign.

Upvotes: 1

Views: 6440

Answers (4)

Bentoy13
Bentoy13

Reputation: 4976

Try grep -Eo '(\.[0-9]{1,3}){4}\s*=.*$' | sed -r 'sed -r 's/\s*=[^:]+:/=/;s/^\.//'

First part is to isolate the end of the line with a good address followed with =; the second part with sed erases any string between = and :, and erases also the first dot before IPv4 address. For compactness, grep is searching for 4 times a dot followed with at most 3 digits.

Upvotes: 0

jxh
jxh

Reputation: 70422

With sed:

snmpwalk -v2c -c community 192.168.122.15 .1.3.6.1.4.1.14823.2.2.1.4.1.2.1.39 \
| sed -e 's/.*\.\([0-9]\+\(\.[0-9]\+\)\{3\}\).*\(".*"\)/\1=\3/'

Or with bash proper:

snmpwalk -v2c -c community 192.168.122.15 .1.3.6.1.4.1.14823.2.2.1.4.1.2.1.39 \
| while read a b c; do echo ${a#${a%*.*.*.*.*}.}=\"${c#*\"}; done

Upvotes: 1

tripleee
tripleee

Reputation: 189497

If the prefix is completely static, just remove it.

result=$(snmpwalk -v2c -c community 192.168.122.15 .1.3.6.1.4.1.14823.2.2.1.4.1.2.1.39)
result=${result#'SNMPv2-SMI::enterprises.14823.2.2.1.4.1.2.1.39.252.200.151.233.54.69.'}
echo "${result/ = STRING: /}"

Or you could do

oldIFS=$IFS
IFS=' .'
set $($(snmpwalk -v2c -c community 192.168.122.15 .1.3.6.1.4.1.14823.2.2.1.4.1.2.1.39)
IFS=$oldIFS
shift 16
result="$1.$2.$3.$4=$7"

The numeric argument to shift and the ${var/str/subst} construct are Bashisms.

Upvotes: 2

dogbane
dogbane

Reputation: 274650

Pipe through sed as shown below:

$ snmpwalk -v2c -c community 192.168.122.15 .1.3.6.1.4.1.14823.2.2.1.4.1.2.1.39 | sed -r 's/.*\.([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+) = .*: (.*)/\1=\2/g'
197.39.5.77="Android"

Upvotes: 0

Related Questions