Convert snmp octet string to human readable date format

后端 未结 3 608
抹茶落季
抹茶落季 2021-02-09 02:36

Using the pysnmp framework i get some values doing a snmp walk. Unfortunately for the oid

1.3.6.1.21.69.1.5.8.1.2 (DOCS-CABLE-DEVICE-MIB)

3条回答
  •  旧时难觅i
    2021-02-09 03:29

    @Paulo Scardine: This was the best answer I found online when working to resolve a very similar problem. It still took me a little while to resolve my issue even with this answer, so I wanted to post a follow up answer that may add more clarity. (specifically the issue with the date having different length options).

    The following piece of code connects to a server and grabs the system time and then outputs it as a string to illustrate the method.

    import netsnmp
    import struct
    oid = netsnmp.Varbind('hrSystemDate.0')
    resp = netsnmp.snmpget(oid, Version=1, DestHost='', Community='public')
    oct = str(resp[0])
    # hrSystemDate can be either 8 or 11 units in length.
    oct_len = len(oct)
    fmt_mapping = dict({8:'>HBBBBBB', 11:'>HBBBBBBcBB'})
    if oct_len == 8 or oct_len == 11:
        t = struct.unpack(fmt_mapping[oct_len], oct)
        print 'date tuple: %s' % (repr(t))
    else:
        print 'invalid date format'
    

    I hope this helps other people who are having similar issues trying to work with this type of data.

提交回复
热议问题