Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I've checked the documentation but can't see anything -- does anyone know if its possible to escape the newlines returned from some OIDs?

I need to parse the output in a script and its causing me headaches.

For example:

$ snmpwalk -v2c -c public -Oq 10.0.0.1 1.3.6.1.2.1.1
SNMPv2-MIB::sysDescr.0 Cisco Internetwork Operating System Software
IOS (tm) Catalyst 4000 L3 Switch Software (cat4000-I5S-M), Version 12.2(18)EW4, RELEASE SOFTWARE (fc1)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2005 by cisco Systems, Inc.
Com
SNMPv2-MIB::sysObjectID.0 SNMPv2-SMI::enterprises.9.1.502
DISMAN-EVENT-MIB::sysUpTimeInstance 122:19:10:46.16
SNMPv2-MIB::sysContact.0 012345
SNMPv2-MIB::sysName.0 name
SNMPv2-MIB::sysLocation.0 London,uk,emea

In my code I can parse each line of the output and split it on the first space which works until I get a multiline response like the sysDescr OID above. It would be great if there was some way to actually print \n in the response instead of a real new line.

share|improve this question
add comment

1 Answer

You can try piping your output through this sed script:

$ snmpwalk ... | sed -n '/MIB::/!{H;$g};/MIB::/{$!x;s/\n/\\n/g;ta;${x;/^$/!p;x};:a;/^$/!p}'
SNMPv2-MIB::sysDescr.0 Cisco Internetwork Operating System Software\nIOS (tm) Catalyst 4000 L3 Switch Software (cat4000-I5S-M), Version 12.2(18)EW4, RELEASE SOFTWARE (fc1)\nTechnical Support: http://www.cisco.com/techsupport\nCopyright (c) 1986-2005 by cisco Systems, Inc.\nCom
SNMPv2-MIB::sysObjectID.0 SNMPv2-SMI::enterprises.9.1.502
DISMAN-EVENT-MIB::sysUpTimeInstance 122:19:10:46.16
SNMPv2-MIB::sysContact.0 012345\nfoo\nbar
SNMPv2-MIB::sysName.0 name
SNMPv2-MIB::sysLocation.0 London,uk,emea

Here's an AWK version that's more readable, but a little longer:

$ snmpwalk ... | awk 'function printit() {printf prev; if (accum) printf "\\n" accum; print ""} !/MIB::/{accum=accum "\\n" $0} /MIB::/{if (NR>1) {printit()}; accum=""; prev=$0} END{printit()}'
share|improve this answer
    
Thank you, Dennis. –  Chris Oct 31 '10 at 14:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.