解析snmpwalk的输出 [英] Parsing snmpwalk output

查看:738
本文介绍了解析snmpwalk的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这里是关于snmpwalk的解析响应许多问题和答案,但我似乎无法在这里找到有关答案。我不是在文本解析不错,但我尽我所能去学习的东西。

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

获得回应如下:

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"

因为有这种特殊的设备上的多个接口输出是ommited。
我需要通过这个输出来分析,这样我可以得到的只有物理接口。比如出字符串:

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"

我需要仅提取XE-4/2/1等与其他行。 。Basicaly,后点之前字符一切请注意线路是这样的:

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"

我的最终目标是把所有物理接口中提取出这种特殊的设备。关于接口的唯一OID似乎iso.3.6.1.2.1.2.2.1.2和集中设备时,它输出的所有接口,包括物理和逻辑。

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.

非常感谢

推荐答案

使用sed命令

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

编辑:使用上面sed命令说明

我用作为的sed的delimiter..You可以使用任何其他字符,如%,〜等等......这是明智的使用字符这并不在你的图案或冲突的任何字符变量

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: - 模式与字符串相匹配 ISO 后跟任意字符,后跟字符串 = STRING:

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

\\\\([^] \\ + \\\\) - 这是上面的..这部分 [格局的延续^ ] \\ + 匹配一个或多个字符不。在 \\ + 表示1个或多个字符。注意我enlose这片模式由 \\(\\)。这是因为我想SED记住这pattern..since这是我包围第一模式,sed的定义这种模式为 \\ 1 。想起 \\ 1 作为第一个模式的变量名字的模式,经过这么 ISO * = STRING: sed中搜索所有的字符不是并记住它,这就是存储模式我们answer.This我想sed来替换整行。

\\([^".]\+\\) - 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.

* - 相匹配的标记模式之后零个或多个字符。为什么我用这个的原因是,它匹配行的剩余和替换标记的图案全行,即 \\ 1

.* - 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.

使用awk命令

awk -v FS="( \"|[.][0-9]+\"$|\"$)" '$0~/iso.*= STRING: "/{print $2}' my_file

这篇关于解析snmpwalk的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆