gpt4 book ai didi

bash - 期望脚本模拟SNMP

转载 作者:行者123 更新时间:2023-11-29 09:14:56 28 4
gpt4 key购买 nike

我想监控不支持 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

最佳答案

终于解决了:) 问题出在路径文件上。

代替相对路径:
设置文件“./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

关于bash - 期望脚本模拟SNMP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12902649/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com