如何获取网络设备oid号

一、安装snmpwalk工具

net-snmp net-snmp-utils
#前一个用来开启snmp服务,后一个是snmp工具

二、如何使用

snmpwalk -v 版本 -c 团体名 ip .1.3.6.1.2.1.1.2.0 #snmp联通的情况下,可以获取网络设备具体型号对应的oid号

三、为何用后面跟的那一串数字

snmpwalk -v 2c -c public 192.168.119.20 1 2> /tmp/demo.txt
less /tmp/demo.txt
找到具体的对应oid号的前面的oid,就是.3.6.1.2.1.1.2.0,但是我们需要在前面加.1,否则无法识别,snmp的oid最上面固定为1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您可以使用第三方库,如pysnmp,来编写脚本。这个库可以帮助您通过SNMP协议访问网络设备的MIB数据。以下是一个示例代码,该代码使用pysnmp库来获取网络设备的CPU利用率: ``` from pysnmp.hlapi import * def get_cpu_utilization(ip, community): errorIndication, errorStatus, errorIndex, varBinds = next( getCmd(SnmpEngine(), CommunityData(community), UdpTransportTarget((ip, 161)), ContextData(), ObjectType(ObjectIdentity('1.3.6.1.2.1.25.3.3.1.2'))) ) if errorIndication: print(errorIndication) elif errorStatus: print('%s at %s' % (errorStatus.prettyPrint(), errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) else: for varBind in varBinds: return int(varBind[1]) if __name__ == "__main__": ip = "192.168.0.1" community = "public" cpu_utilization = get_cpu_utilization(ip, community) print("CPU Utilization:", cpu_utilization) ``` 请注意,上面的代码仅作为示例,可能需要根据您的实际网络设备和需求进行调整。 ### 回答2: 写一个通过MIB获取网络设备CPU利用率的Python脚本可以借助第三方库pysnmp来实现。以下是一个简单的示例脚本。 ```python from pysnmp.hlapi import * def get_cpu_utilization(ip, community): # 定义OID oid_cpu_utilization = ObjectType(ObjectIdentity('.1.3.6.1.4.1.2021.11.11.0')) # 创建SNMP Engine engine = SnmpEngine() # 创建CommunityData实体 community_data = CommunityData(community) # 创建UdpTransportTarget实体 transport = UdpTransportTarget((ip, 161)) # 创建ContextData实体 context_data = ContextData() # 发起SNMP GET请求 error_indication, error_status, error_index, var_binds = next( getCmd(engine, community_data, transport, context_data, oid_cpu_utilization) ) # 处理SNMP GET响应 if error_indication: print(f"SNMP GET请求错误:{error_indication}") elif error_status: print(f"SNMP GET响应错误:{error_status.prettyPrint()} at {error_index and var_binds[int(error_index)-1][0] or '?'}") else: for var_bind in var_binds: cpu_utilization = var_bind[1].prettyPrint() print(f"网络设备CPU利用率: {cpu_utilization}") # 使用示例 ip_address = '192.168.1.1' # 网络设备的IP地址 snmp_community = 'public' # SNMP团体名 get_cpu_utilization(ip_address, snmp_community) ``` 以上脚本使用pysnmp库来进行SNMP的GET请求,并从OID `.1.3.6.1.4.1.2021.11.11.0`获取网络设备的CPU利用率。在脚本中,需要提供网络设备的IP地址和SNMP团体名作为参数。执行脚本后,将输出获取到的CPU利用率。请注意,此示例中只获取了一个OID的值,你可能需要根据你的网络设备和具体需求修改OID和其他参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值