期望脚本模拟SNMP

时间:2012-10-15 19:28:13

标签: bash snmp expect

我想要监控不支持SNMP的设备,所以我尝试通过expect脚本获取计数器。此脚本使用SSH连接到设备,将输出记录在文件上,然后解析输出以获得所需的计数器。

当我从控制台执行脚本时,我得到以下所需的输出:

root@box:/path# ./GGSN-PDP-Contexts.expect  
.1.3.6.1.4.1.6147.2.1  
Integer32  
310838  

但是,当我尝试使用snmpget获取结果时,它不起作用!

root@box:/path# snmpget -m TDP-MIB  -v 2c -c TM_Com_Pub localhost .1.3.6.1.4.1.6147.2.1  
TDP-MIB::PDPContextsNumber = No Such Instance currently exists at this OID  

顺便说一句,这是snmpd.conf中的相关配置:
    传递.1.3.6.1.4.1.6147.2.1 / usr / bin / expect /path/GGSN-PDP-Contexts.expect

这是我正在使用的期望脚本:

#!/usr/bin/expect -f  

# Constants  
set user "user"  
set device "10.10.222.176"  
set pass "blablabla"  
set timeout -1  
set prompt "GGSN-LV02#"  
set file "./GGSN-PDP-Contexts.log"  

# Options  
match_max 100000  
log_user 0  

# Access to device  
spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no $user@$device  
expect "*?assword:*"  
send -- "$pass\r"  

# Commands execution  
expect -exact "$prompt"  
send -- "display pdp-number\r"  
log_file -a $file

# Logging  
expect -exact "$prompt"  
log_file  
send -- "quit\r"  

# Get the value  
set result [exec cat $file | grep "ALL GTP" | cut -d " " -f14]  
set value [format %d $result]  

# Print the value  
puts ".1.3.6.1.4.1.6147.2.1"  
puts "Integer32"  
puts $value  # If I replace the $value with a number, it doesn't work either  

# Erase log file  
exec rm $file  
close  
你可以给我任何暗示吗?提前谢谢!

编辑:

此外,这些是snmpget调试输出的最后一行:

trace: snmp_comstr_parse(): snmp_auth.c, 135:  
dumph_recv:   SNMP version  
dumpx_recv:    02 01 01 
dumpv_recv:      Integer:       1 (0x01)  
trace: snmp_comstr_parse(): snmp_auth.c, 147:  
dumph_recv:   community string  
dumpx_recv:    04 0A 54 4D 5F 43 6F 6D 5F 50 75 62   
dumpv_recv:      String:        TM_Com_Pub  
trace: _snmp_parse(): snmp_api.c, 4149:  
dumph_recv:   PDU  
trace: snmp_pdu_parse(): snmp_api.c, 4255:  
dumpv_recv:     Command RESPONSE  
trace: snmp_pdu_parse(): snmp_api.c, 4336:  
dumph_recv:     request_id  
dumpx_recv:      02 04 3B 9E CF 74   
dumpv_recv:        Integer:     1000263540 (0x3B9ECF74)  
trace: snmp_pdu_parse(): snmp_api.c, 4347:  
dumph_recv:     error status  
dumpx_recv:      02 01 00   
dumpv_recv:        Integer:     0 (0x00)  
trace: snmp_pdu_parse(): snmp_api.c, 4358:  
dumph_recv:     error index  
dumpx_recv:      02 01 00   
dumpv_recv:        Integer:     0 (0x00)  
trace: snmp_pdu_parse(): snmp_api.c, 4376:  
dumph_recv:     VarBindList  
trace: snmp_pdu_parse(): snmp_api.c, 4406:  
dumph_recv:       VarBind  
trace: snmp_parse_var_op(): snmp.c, 166:  
dumph_recv:         Name  
dumpx_recv:          06 09 2B 06 01 04 01 B0 03 02 01   
dumpv_recv:            ObjID: TDP-MIB::PDPContextsNumber  
trace: snmp_pdu_parse(): snmp_api.c, 4415:  
dumph_recv:         Value  
TDP-MIB::PDPContextsNumber = No Such Instance currently exists at this OID  

此外,这是我目前的MIB:

TDP-MIB DEFINITIONS ::= BEGIN  

IMPORTS  
    MODULE-IDENTITY, OBJECT-TYPE, Integer32, enterprises  
                    FROM SNMPv2-SMI  
    OBJECT-GROUP    FROM SNMPv2-CONF;   

TDP MODULE-IDENTITY  
    LAST-UPDATED "201210080000Z" -- 8/oct/2012  
    ORGANIZATION    "TELEFONICA"  
    CONTACT-INFO    "Authors: Hernan Romano / Antonio Ocampo  
                     Email: h.romanoc@pucp.edu.pe / aocampo@pucp.edu.pe"  
    DESCRIPTION     "MIB para gestionar los equipos que carecen de SNMP"  
    REVISION        "201210080000Z" -- 08/oct/2012  
    DESCRIPTION     "Revision 2.1"  
    ::= { enterprises 6147 }    

Nokia                   OBJECT IDENTIFIER ::= { TDP 1 }  
Huawei                  OBJECT IDENTIFIER ::= { TDP 2 }  
TDPMIBConformance       OBJECT IDENTIFIER ::= { TDP 3 }  

ClearCodeGroup1         OBJECT-TYPE  
                        SYNTAX Integer32  
                        MAX-ACCESS read-only  
                        STATUS current  
                        DESCRIPTION "Clear Code Group 1"  
                        ::= { Nokia 1 }  

PDPContextsNumber       OBJECT-TYPE  
                        SYNTAX Integer32  
                        MAX-ACCESS read-only  
                        STATUS current  
                        DESCRIPTION "PDP Contexts Number"  
                        ::= { Huawei 1 }  

TDPMIBGroup             OBJECT IDENTIFIER  
                        ::= { TDPMIBConformance 1 }  

--grupoTDP      OBJECT-GROUP  
--      OBJECTS {  
--              ClearCodeGroup1,  
--              PDPContextsNumber  
--      }  
--      STATUS current  
--      DESCRIPTION "Objetos para el monitoreo de los equipos que carecen de SNMP"  
--      ::= { TDPMIBGroup 1 }  
END  

1 个答案:

答案 0 :(得分:2)

终于解决了:)问题是路径文件。

而不是相对路径:
设置文件“./GGSN-PDP-Contexts.log”

我把绝对路径:
设置文件“/FULL_PATH/GGSN-PDP-Contexts.log”

和snmpget有效!!

root@box:/path# snmpget -m TDP-MIB  -v 2c -c TM_Com_Pub localhost .1.3.6.1.4.1.6147.2.1  
TDP-MIB::PDPContextsNumber = INTEGER: 319291