0

I know here are many questions and answers in regards to parsing snmpwalk response but I cant seem to find the relating answer here. I am not good in parsing texts but I am doing my best to learn things.

I am polling my Juniper device with command:

snmpwalk -v2c -c blablabla x.x.x.x iso.3.6.1.2.1.2.2.1.2

Getting response as follow:

iso.3.6.1.2.1.2.2.1.2.2548 = STRING: "xe-4/0/1.3461"
iso.3.6.1.2.1.2.2.1.2.2549 = STRING: "xe-4/0/2.3462"
iso.3.6.1.2.1.2.2.1.2.2550 = STRING: "xe-4/0/2.3461"
iso.3.6.1.2.1.2.2.1.2.2551 = STRING: "xe-4/0/3.3462"
iso.3.6.1.2.1.2.2.1.2.2552 = STRING: "xe-4/0/3.3461"
iso.3.6.1.2.1.2.2.1.2.2557 = STRING: "xe-4/2/1.1514"
iso.3.6.1.2.1.2.2.1.2.2558 = STRING: "xe-4/2/1.1634"

Output is ommited as there are many interfaces on this particular device. I need to parse through this output so that I can get only physical interfaces. For instance out of string:

iso.3.6.1.2.1.2.2.1.2.2557 = STRING: "xe-4/2/1.1514"

I need to extract only xe-4/2/1 and so on with other lines. Basicaly, after character " everything before dot. Please note that there lines like this:

iso.3.6.1.2.1.2.2.1.2.1664 = STRING: "xe-4/3/3"
iso.3.6.1.2.1.2.2.1.2.1665 = STRING: "xe-4/1/3.534"
iso.3.6.1.2.1.2.2.1.2.1666 = STRING: "xe-4/1/3.552"
iso.3.6.1.2.1.2.2.1.2.1667 = STRING: "xe-4/3/0.1613"
iso.3.6.1.2.1.2.2.1.2.1296 = STRING: "ae2.1464"
iso.3.6.1.2.1.2.2.1.2.1297 = STRING: "ae2.1503"
iso.3.6.1.2.1.2.2.1.2.1299 = STRING: "ae2.1596"
iso.3.6.1.2.1.2.2.1.2.1300 = STRING: "ae2.2020"

My final goal is to extract all physical interfaces out of this particular device. The only OID relating to interfaces seems to be iso.3.6.1.2.1.2.2.1.2 and when pooling the device it outputs all interfaces including physical and logical.

Many Thanks

  • The OID you mentioned is `ifDescr` and as you noticed it displays all existing interfaces. You can walk trough `ifType` OID (`1.3.6.1.2.1.2.2.1.3`) filter interfaces based on its type and then map them to interface names from `ifDescr` OID. – pajaja Feb 02 '15 at 18:11
  • Why not just run `snmpget -v2c -c blablabla x.x.x.x iso.3.6.1.2.1.2.2.1.2.2557` `snmpget` for one `ODI`. `snmpwalk` is used to get group of `ODIs` – Jotne Feb 02 '15 at 19:54

2 Answers2

0

You can use this sed command:

sed -i.bak 's/\([^"]*"[^.]*\).*/\1"/' file
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

using sed command

sed 's#iso.*= STRING: "\([^".]\+\).*#\1#g' my_file

EDIT: Explanation using sed command above

The #

I use # as sed's delimiter..You can use any other character such as "%", "~" etc...It is wise to use a character that does not conflict any character in your pattern or variable

iso.*= STRING: " - matches a pattern with the string iso followed by any character followed by the string =STRING: "

\\([^".]\+\\) - this is the continuation of the pattern above.. this part [^".]\+ matches one or more character that is not " or .. The \+ means 1 or more character. Note I enlose this piece of pattern by \( \). This is because I want sed to remember this pattern..since this is the first pattern I am enclosing, sed defines this pattern as \1. think of \1 as a variable name for the first pattern. SO after the pattern iso.*= STRING: " sed searches for all characters that is not " or . and remember it, which is our answer.This stored pattern I want sed to replace the whole line with.

.* - matches zero or more character after the marked pattern. The reason why I use this is that it matches the remainder of the line and replace the whole line with the marked pattern, that is \1.

Note: This will only work if you match the whole line with a pattern and then mark part of the pattern in your regex for replacement. if the entire pattern is not found no replacement occurs.

using awk command

awk -v FS="( \"|[.][0-9]+\"$|\"$)" '$0~/iso.*= STRING: "/{print $2}' my_file
repzero
  • 8,254
  • 2
  • 18
  • 40
  • Thanks Xorg. Both do what I wanted. I would like to focus on "sed" example. If you dont mind I will ask you several more questions. So, breaking down the line "'s#iso.*= STRING: "\([^".]\+\).*#\1#g'" I want to go through it with your help as I really want to understand what happens under the hood. – jeronimo777 Feb 03 '15 at 18:23
  • I think I do get some part of it as follow: The "s" means to search the input, however I dont know what "#" means. Next it mathes the string "iso". Next ".*". The . means any character, and the * means to match the previous expression 0 or more times. So, overall ".*" means match anything after "iso". So far we have matched "iso.3.6.1.2.1.2.2.1.2.1665". Next we have "= STRING: "" So up to this point we have matched "iso.3.6.1.2.1.2.2.1.2.1665 = STRING: "" – jeronimo777 Feb 03 '15 at 18:26
  • The rest is a bit hard for me. The "\(" says to remember everything until the next "\)" then the \1 tells sed to replace what we found with "\1". \1 evaluates to whatever was matched in the first set of "\(" and "\)" tags So, I dont get "s#", "[^".]\+", also it would be nice if you could explain expression ".*#\1#g" Thanks very much for your help. – jeronimo777 Feb 03 '15 at 18:26