问题
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
回答1:
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
回答2:
You can use this sed command:
sed -i.bak 's/\([^"]*"[^.]*\).*/\1"/' file
来源:https://stackoverflow.com/questions/28283750/parsing-snmpwalk-output