LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-07-2010, 10:31 AM   #1
shippropeller
LQ Newbie
 
Registered: Jul 2010
Location: Zurich
Posts: 5

Rep: Reputation: 0
BASH: output of snmpget with multiple OIDs into separate variables


Hi all

I have a problem with snmp answers being empty or having spaces.

What I already have:

#get all interface indexes (if you wonder - I'm working for a cable company and different cablemodems have different number and types of interfaces):
ifi=$(for i in $(snmpwalk -Oqv -v 2c -c $COMMUNITY $IP .1.3.6.1.2.1.2.2.1.1); do echo -n "$i "; done)

#for every interface I want to have the status (admin and oper), speed, type, physical address (MAC address) and description
#the printf command gives me a nice table of all the interfaces and their respective values
for i in ${ifi}
do
read -d "\e" ifadm ifoper ifspeed iftype ifphys < <(snmpget -Oqv -v 2c -c $COMMUNITY $IP .1.3.6.1.2.1.2.2.1.7.$i .1.3.6.1.2.1.2.2.
1.8.$i .1.3.6.1.2.1.2.2.1.5.$i .1.3.6.1.2.1.2.2.1.3.$i .1.3.6.1.2.1.2.2.1.6.$i)
ifdescr=$(snmpget -Oqv -v 2c -c $COMMUNITY $IP .1.3.6.1.2.1.2.2.1.2.$i)
printf "%+3s |%+5s |%+8s | %+10s | %-40s | %-20s | %s\n" "${i}" "${ifadm}" "${ifoper}" "${ifspeed}" "${ifdescr}" "${iftype}" "${if
phys}"
done

The problem is the physical address which is sometimes empty and the description which has spaces.
So I'm doing 2 snmpgets which is slower than 1 snmpget (sometimes I have up to 18 interfaces).


I'm trying to explain it a bit simpler.
Interface 5 gives me back the following lines:

user@host:~$ snmpget -Oqv -v 2c -c $COMMUNITY $IP .1.3.6.1.2.1.2.2.1.7.5 .1.3.6.1.2.1.2.2.1.8.5 .1.3.6.1.2.1.2.2.1.5.5 .1.3.6.1.2.1.2.2.1.3.5 .1.3.6.1.2.1.2.2.1.6.5 .1.3.6.1.2.1.2.2.1.2.5
up
down
1000000000
ethernetCsmacd

Ethernet CPE Interface


Now the first line should go into variable ifadm,
2nd line should go into variable ifoper,
3rd line should go into variable ifspeed,
4th line should go into variable iftype,
5th line (which is empty) should go into variable ifphys and finally
6th line (which has spaces) should go into variable ifdescr

I'm pretty sure I made it too complicated, but I don't see the simple solution.

Many thx to all the help.
 
Old 07-07-2010, 01:19 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Sorry, it is a bit hard to understand exactly what you're doing. It would help readability a lot if you put [code][/code] tags around your code, to preserve formatting.

Are the entries all coming out as one-per-line? And it does output blank lines if the value is empty? If so, then you can simply set the IFS to newline, then each variable can be easily configured to catch one whole line of code. You might even consider using an array here instead. Something like:
Code:
IFS=$'\n'   #set separator to newline only

ifarray=( $(your-command-to-get-the-values) )

ifadm="${ifarray[0]}"
ifoper="${ifarray[1]}"
...etc

unset IFS

Once you have the variables properly set, there should be no problem in handling blanks, if required. Simple parameter substitution can be used to remove or convert spaces, and to output a default value if blank.
 
1 members found this post helpful.
Old 07-07-2010, 03:02 PM   #3
shippropeller
LQ Newbie
 
Registered: Jul 2010
Location: Zurich
Posts: 5

Original Poster
Rep: Reputation: 0
Thumbs up

Sorry for posting hard to read code. I think you can look at the one-liner code which was at the bottom of the first post, the rest was "just" explanation:

Command:
Code:
user@host:~$ snmpget -Oqv -v 2c -c $COMMUNITY $IP .1.3.6.1.2.1.2.2.1.7.5 .1.3.6.1.2.1.2.2.1.8.5 .1.3.6.1.2.1.2.2.1.5.5 .1.3.6.1.2.1.2.2.1.3.5 .1.3.6.1.2.1.2.2.1.6.5 .1.3.6.1.2.1.2.2.1.2.5
Output:
Code:
up
down
1000000000
ethernetCsmacd

Ethernet CPE Interface
Yes, all entries are coming out as one-per-line. And yes, it does output blank lines if the value is empty.

I already thank you a lot for your time and I think you are close nearby, but the blank line isn't handled as expected:

Command:
Code:
user@host:~$ IFS=$'\n' ifarray=( $(snmpget -Oqv -v 2c -c $COMMUNITY $IP .1.3.6.1.2.1.2.2.1.7.5 .1.3.6.1.2.1.2.2.1.8.5 .1.3.6.1.2.1.2.2.1.5.5 .1.3.6.1.2.1.2.2.1.3.5 .1.3.6.1.2.1.2.2.1.6.5 .1.3.6.1.2.1.2.2.1.2.5) ); echo =0=${ifarray[0]}=; echo =1=${ifarray[1]}=; echo =2=${ifarray[2]}=; echo =3=${ifarray[3]}=; echo =4=${ifarray[4]}=; echo =5=${ifarray[5]}=
Output:
Code:
=0=up=
=1=down=
=2=1000000000=
=3=ethernetCsmacd=
=4=Ethernet CPE Interface=
=5==
So it is assigning line 6 to array index 4 insted of 5 as expected.

Well, I could do simply after the my-command-to-get-the-values:
Code:
 | sed 's/ˆ$/ /'
Or do you know here a better trick?

Once more: Many thx for your help.
 
Old 07-07-2010, 03:52 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Hmm. I guess I was wrong about that then. I was pretty sure the IFS-as-newline would register during the array setting. Sorry.

I think your idea of using sed at the end is a reasonable one. I can't think of any other easy way to register the field as non-empty off the top of my head. I think I'd use a unique character or string instead of a single space though, which makes it a bit easier to substitute out again afterwards.

And by the way, you can shorten up that nasty number string with a bit of brace expansion. Or else set it ahead of time in a variable. It helps keep the code readable that way.

Code:
IFS=$'\n'

ifarray=( $(snmpget -Oqv -v 2c -c $COMMUNITY $IP .1.3.6.1.2.1.2.2.1.{2,3,5,6,7,8}.5 |sed 's/^$/empty/ ) )

echo "${ifarray[4]/empty/}"  #removes the "empty" string if it finds it
 
1 members found this post helpful.
Old 07-08-2010, 12:50 AM   #5
shippropeller
LQ Newbie
 
Registered: Jul 2010
Location: Zurich
Posts: 5

Original Poster
Rep: Reputation: 0
Many thx for all your help!
 
  


Reply

Tags
bash, lines, separate, variables



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I create multiple variables from a list in Bash? Passions Programming 12 05-23-2009 05:23 PM
Bash and snmpget seefor Programming 2 04-01-2009 01:16 PM
ambiguous output using variables in bash gwong Linux - Newbie 2 05-05-2007 08:49 PM
bash: random creation of multiple variables vicious1 Programming 3 04-23-2007 03:34 AM
BASH script – reading and writing variables to a separate file morrolan Programming 10 09-20-2005 07:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration