Convert snmp octet string to human readable date f

2019-03-27 07:19发布

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)

i get a weird result which i cant correctly print here since it contains ascii chars like BEL ACK

When doing a repr i get:

OctetString('\x07\xd8\t\x17\x03\x184\x00')

But the output should look like:

2008-9-23,3:24:52.0

the format is called "DateAndTime". How can i translate the OctetString output to a "human readable" date/time ?

3条回答
Evening l夕情丶
2楼-- · 2019-03-27 07:57

@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='<ip>', 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.

查看更多
甜甜的少女心
3楼-- · 2019-03-27 07:57

Shameless plug here: The Pycopia SNMP and SMI modules correctly handle this object, and others as well. Pycopia is installed from source, and dont forget the mibs file if you try it.

查看更多
唯我独甜
4楼-- · 2019-03-27 08:11

The format is here.

A date-time specification. 
            field  octets  contents                  range
            -----  ------  --------                  -----
              1      1-2   year*                     0..65536
              2       3    month                     1..12
              3       4    day                       1..31
              4       5    hour                      0..23
              5       6    minutes                   0..59
              6       7    seconds                   0..60
                           (use 60 for leap-second)
              7       8    deci-seconds              0..9
              8       9    direction from UTC        '+' / '-'
              9      10    hours from UTC*           0..13
             10      11    minutes from UTC          0..59
* Notes:
            - the value of year is in network-byte order
            - daylight saving time in New Zealand is +13 For example, 
              Tuesday May 26, 1992 at 1:30:15 PM EDT would be displayed as:
                 1992-5-26,13:30:15.0,-4:0 
              Note that if only local time is known, then timezone
              information (fields 8-10) is not present.

You can use struct.unpack:

>>> import struct, datetime
>>> s = '\x07\xd8\t\x17\x03\x184\x00'
>>> datetime.datetime(*struct.unpack('>HBBBBBB', s))
datetime.datetime(2008, 9, 23, 3, 24, 52)
查看更多
登录 后发表回答