下面是我发布的snmpwalk命令,无论它显示什么结果,都是我必须匹配的。
当我尝试将结果存储在一个数组中然后尝试匹配它时,工作正常。
它是这样的:
snmpwalk -mALL -v2c -cpublic 10.126.143.249 ifname **=====>command which i issue**
这是它给出的结果(我必须匹配)
IF-MIB::ifName.2 = STRING: port ethernet 1/1
IF-MIB::ifName.3 = STRING: port ethernet 1/2
IF-MIB::ifName.67108865 = STRING: SKB
IF-MIB::ifName.67108866 = STRING: i1
IF-MIB::ifName.134217732 = STRING: LINK
IF-MIB::ifName.134217735 = STRING: port ethernet 1/1 dot1q pvc 200 1/2/7
=============================================== ============================================
以下是我尝试过的代码: sub snmpwalk_ifName(){
my $rs;
my @array=("IF-MIB::ifName.* = STRING: port ethernet *
IF-MIB::ifName.* = STRING: port ethernet *
IF-MIB::ifName.* = STRING: SKB
IF-MIB::ifName.* = STRING: i1
IF-MIB::ifName.* = STRING: LINK
IF-MIB::ifName.* = STRING: port ethernet * dot1q pvc 200 *");
my%out = $ :: device2-> send_cmds(" snmpwalk -mALL -v2c -cpublic $ :: DEVICE1 {ADMIN_IP} ifname");
$ rs = Match :: Match_Output(
OUTPUT => $out{OUTPUT},
EXP_RESULT => @array);
$ :: test-> checkPoint(RESULT => $ rs,
MSG => "CHECKPOINT for verifying snmpwalk output");
}
答案 0 :(得分:0)
不确定你想要匹配什么,但这里有一个oneliner,希望能让你思考(玩起来很有趣): 初始输出:
snmpwalk -mALL -v2c -c lab 192.168.1.65 ifname ~
IF-MIB::ifName.4 = STRING: lsi
IF-MIB::ifName.5 = STRING: dsc
IF-MIB::ifName.6 = STRING: lo0
IF-MIB::ifName.7 = STRING: tap
IF-MIB::ifName.8 = STRING: gre
IF-MIB::ifName.9 = STRING: ipip
IF-MIB::ifName.10 = STRING: pime
IF-MIB::ifName.11 = STRING: pimd
IF-MIB::ifName.12 = STRING: mtun
IF-MIB::ifName.16 = STRING: lo0.0
IF-MIB::ifName.21 = STRING: lo0.16384
IF-MIB::ifName.22 = STRING: lo0.16385
IF-MIB::ifName.248 = STRING: lo0.32768
IF-MIB::ifName.501 = STRING: pp0
IF-MIB::ifName.502 = STRING: st0
IF-MIB::ifName.503 = STRING: ppd0
IF-MIB::ifName.504 = STRING: ppe0
IF-MIB::ifName.505 = STRING: vlan
IF-MIB::ifName.506 = STRING: ge-0/0/0
IF-MIB::ifName.507 = STRING: ge-0/0/1
IF-MIB::ifName.508 = STRING: ge-0/0/0.0
IF-MIB::ifName.509 = STRING: sp-0/0/0
IF-MIB::ifName.510 = STRING: sp-0/0/0.0
IF-MIB::ifName.511 = STRING: gr-0/0/0
IF-MIB::ifName.512 = STRING: sp-0/0/0.16383
IF-MIB::ifName.513 = STRING: ip-0/0/0
IF-MIB::ifName.514 = STRING: lsq-0/0/0
IF-MIB::ifName.515 = STRING: mt-0/0/0
IF-MIB::ifName.516 = STRING: lt-0/0/0
IF-MIB::ifName.517 = STRING: ge-0/0/1.0
IF-MIB::ifName.518 = STRING: ge-0/0/2
IF-MIB::ifName.519 = STRING: ge-0/0/2.0
IF-MIB::ifName.520 = STRING: ge-0/0/3
IF-MIB::ifName.521 = STRING: ge-0/0/4
IF-MIB::ifName.522 = STRING: ge-0/0/3.0
将接口名称放入一个带有一个衬垫的数组中:
snmpwalk -mALL -v2c -c lab 192.168.1.65 ifname | \
perl -wnE 'push @each_line,(split " ",$_)[-1];END {say $_ for @each_line}'
输出:
lsi
dsc
lo0
tap
gre
ipip
pime
pimd
mtun
lo0.0
lo0.16384
lo0.16385
lo0.32768
pp0
st0
ppd0
ppe0
vlan
ge-0/0/0
ge-0/0/1
ge-0/0/0.0
sp-0/0/0
sp-0/0/0.0
gr-0/0/0
sp-0/0/0.16383
ip-0/0/0
lsq-0/0/0
mt-0/0/0
lt-0/0/0
ge-0/0/1.0
ge-0/0/2
ge-0/0/2.0
ge-0/0/3
ge-0/0/4
ge-0/0/3.0
或索引+名称:
snmpwalk -mALL -v2c -c lab 192.168.1.65 ifname |
perl -wnE 'say "index = $1 , Int Name = $2" if /ifName\.(\d+).*?:\s(.*)/'
输出:
index = 4 , Int Name = lsi
index = 5 , Int Name = dsc
index = 6 , Int Name = lo0
index = 7 , Int Name = tap
index = 8 , Int Name = gre
index = 9 , Int Name = ipip
index = 10 , Int Name = pime
index = 11 , Int Name = pimd
index = 12 , Int Name = mtun
index = 16 , Int Name = lo0.0
index = 21 , Int Name = lo0.16384
index = 22 , Int Name = lo0.16385
index = 248 , Int Name = lo0.32768
index = 501 , Int Name = pp0
index = 502 , Int Name = st0
index = 503 , Int Name = ppd0
index = 504 , Int Name = ppe0
index = 505 , Int Name = vlan
index = 506 , Int Name = ge-0/0/0
index = 507 , Int Name = ge-0/0/1
index = 508 , Int Name = ge-0/0/0.0
index = 509 , Int Name = sp-0/0/0
index = 510 , Int Name = sp-0/0/0.0
index = 511 , Int Name = gr-0/0/0
index = 512 , Int Name = sp-0/0/0.16383
index = 513 , Int Name = ip-0/0/0
index = 514 , Int Name = lsq-0/0/0
index = 515 , Int Name = mt-0/0/0
index = 516 , Int Name = lt-0/0/0
index = 517 , Int Name = ge-0/0/1.0
index = 518 , Int Name = ge-0/0/2
index = 519 , Int Name = ge-0/0/2.0
index = 520 , Int Name = ge-0/0/3
index = 521 , Int Name = ge-0/0/4
index = 522 , Int Name = ge-0/0/3.0
无论如何,perl非常适合酷炫的oneliner解析。