3

I've been working on getting a few simple monitoring tools running at home, and decided to be funny and retrieve the printer data along with everything else, however now that I've got the SNMP portion of it working quite well, I can't seem to be able to parse the data that my SNMPGET command retrieves properly in Linux, the current script I am using is as follows:

#!/usr/bin/env bash

# RegEx for Strings:    "(.+?)"| -?\d+ 
RegExStr='"(.+?)"| -?\d+'

# ***
# Brother HL-2150N Printer
# ***
# Order Data: Toner Naame, Toner Level, Drum Name, Drum Status, Total Pages Printer,  Display Status
Input=$(snmpget -v 1 -c public 192.168.16.112 SNMPv2-SMI::mib-2.43.11.1.1.6.1.1 SNMPv2-SMI::mib-2.43.11.1.1.8.1.1 SNMPv2-SMI::mib-2.43.11.1.1.6.1.2 SNMPv2-SMI::mib- 2.43.11.1.1.9.1.1 SNMPv2-SMI::mib-2.43.10.2.1.4.1.1 SNMPv2-SMI::mib-2.43.16.5.1.2.1.1 -m BROTHER-MIB)

Output1=( $(echo $Input | egrep -o $RegExStr) )

# Output
echo $Input
echo ${Output1[@]}

Which, oddly enough does not work. I'm fairly certain my regular expression ( "(.+?)" ) is correct, as I've tested it numerous times in various different syntax checkers and testers. It's supposed to select all the data that's between quotation marks ("").

Anyhow, the SNMPGET return is:

SNMPv2-SMI::mib-2.43.11.1.1.6.1.1 = STRING: "Black Toner Cartridge" SNMPv2-SMI::mib-2.43.11.1.1.8.1.1 = INTEGER: -2 SNMPv2-SMI::mib-2.43.11.1.1.6.1.2 = STRING: "Drum Unit" SNMPv2-SMI::mib-2.43.11.1.1.9.1.1 = INTEGER: -3 SNMPv2-SMI::mib-2.43.10.2.1.4.1.1 = Counter32: 13630 SNMPv2-SMI::mib-2.43.16.5.1.2.1.1 = STRING: "SLAAP "

I've tried various things myself, and using grep returns a blank string. to my understanding grep does not support every regular expression command by itself, so I started using egrep, while this returns SOMETHING, it is everything inside the original string divided by spaces, starting at the first quotation mark.

Is there anything I'm missing? I've looked around, and adjusted my methods a few times but never seemed to get a usable array in return.

Anyhow, I appreciate any help/pointers you'd be able to give me. I'd like to be able to get this running, even if just for fun and a good learning experience. Thank you in advance though! I'll be fidgeting on with it some more myself, but will check here every now and then.

2 Answers2

1

From your output:

To get all strings:

grep -oP 'STRING: *"\K[^"]*'
Black Toner Cartridge
Drum Unit
SLAAP 

To get all integers:

grep -oP '(INTEGER|Counter32): *\K[^ ]*'
-2
-3
13630
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • I've combined the two lines (and my original script) into: Output1=( $(echo $Input | grep -oP 'STRING: *"\K[^"]*|(INTEGER|Counter32): *\K[^ ]*') ) This seems to work like a charm! It still returns everything beyond a space as a new array piece, but I believe that's something Bash specific. Thank you for the help! I appreciate it. – Silvester van Harberden Oct 03 '13 at 10:43
0

With awk you can do this:

awk 'NR%2==0' RS=\" <<< $Input
Black Toner Cartridge
Drum Unit
SLAAP

Or into a variable

Output1=$(awk 'NR%2==0' RS=\" <<< $Input)
Jotne
  • 40,548
  • 12
  • 51
  • 55
  • This seems to work, although it returns it all into a single entry Input1(0). Not a huge problem, of course. But figured I'd let you know! Appreciate the alternative method however, as I'm still trying to learn how to work the Bash commands. :) – Silvester van Harberden Oct 03 '13 at 10:44