1

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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
Kareem Hamed
  • 21
  • 1
  • 4

4 Answers4

2

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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
1

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
jxh
  • 69,070
  • 8
  • 110
  • 193
  • Thanks, It worked with one of the output only 197.39.5.3="Android" SNMPv2-SMI::enterprises 0.0.0.0=14823 However the original result is SNMPv2-SMI::enterprises.14823.2.2.1.4.1.2.1.39.240.90.9.122.245.208.197.39.7.27 = STRING: "Android" SNMPv2-SMI::enterprises.14823.2.2.1.4.1.2.1.39.240.90.9.149.138.172.197.39.7.47 = STRING: "Android" SNMPv2-SMI::enterprises.14823.2.2.1.4.1.2.1.39.240.231.126.16.33.84.197.39.7.32 = STRING: "Android" SNMPv2-SMI::enterprises.14823.2.2.1.4.1.2.1.39.240.231.126.170.135.132.197.39.7.25 = "" and others – Kareem Hamed Sep 03 '13 at 10:26
0

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"
dogbane
  • 266,786
  • 75
  • 396
  • 414
0

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.

Bentoy13
  • 4,886
  • 1
  • 20
  • 33