简体   繁体   English

如何查询 SNMP 以从 IP 获取 OID 和日期

[英]How to query SNMP to get OID and Date from IP

Hello I have a project in python to try to learn more about things I will be learning later in college and I have written out the steps below.您好,我在 python 有一个项目,试图了解更多关于我稍后将在大学学习的内容,我已经写下了以下步骤。 I know how to read out write to files and export as a csv from a list but running the SNMP query and getting the OID is my problem.我知道如何读出对文件的写入并从列表中导出为 csv 但运行 SNMP 查询并获取 OID 是我的问题。 I am sorry if this is a stupid question or the answer is easily accessible.如果这是一个愚蠢的问题或者答案很容易获得,我很抱歉。

  1. Read in a list of IP addresses from a text file从文本文件中读取 IP 地址列表
  2. Run an SNMP query against each ip address针对每个 ip 地址运行 SNMP 查询
  3. Looking for the value of a specific OID (1.3.6.1.4.1.318.1.1.1.2.2.4.0).查找特定 OID (1.3.6.1.4.1.318.1.1.1.2.2.4.0) 的值。 That OID is a string value (a date, more specifically)该 OID 是一个字符串值(更具体地说,是一个日期)
  4. Write the ip address and the date returned by the query to a different text file (csv format, preferably)将ip地址和查询返回的日期写入不同的文本文件(最好是csv格式)

There are several libraries in Python for performing SNMP queries, but here's one example of how you would perform a query using the snmp library ( pip install snmp ) Python 中有几个库用于执行 SNMP 查询,但这里有一个示例,说明如何使用snmp库执行查询( pip install snmp

from snmp.engine import Engine
from snmp.message import MessageProcessingModel
from snmp.types import OctetString

addrs = [] # populate with your addresses

with Engine() as engine:
    for addr in addrs:
        manager = engine.Manager(addr, version=MessageProcessingModel.SNMPv2c, community=b"public")
        response = manager.get("1.3.6.1.4.1.318.1.1.1.2.2.4.0")
        value = response.variableBindings[0].value

        if isinstance(value, OctetString):
            print(value.data)
        else:
            print(f"Unexpected result: {value}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM