Search code examples
snmppysnmpmib

How to set DateAndTime for HrSystemDate MIB object using PySNMP


I can't set the dateAndTime in the standard mib HrSystemDate when using the pysnmp setCmd. I have no problems setting other mibs with octetstring, the only problem is for the date/time setting which is also an octetstring format. I am not sure what I am missing. Please advise how to set the date/time via pysnmp setCmd.


I can set the date and time using snmpset in windows command by using this value format '2019-04-04,09:18:32.0,+13:0',

e.g. snmpset -v3 -l authPriv -u snmpAdmin -a SHA -A "password" -x AES -X "password" -n terminal 192.168.0.5 .1.3.6.1.2.1.25.1.2.0 = 2019-04-04,09:18:32.0,+13:0

In ireasoning I can also set it on both format, dateandtime and octetstring I can't post a picture yet so here's the link. ireasoning


Snippet

code


def setSnmpV3string(ip, context='terminal', oid='.1.3.6.1.4.1.2509.8.29.2.15.1.2.1',value=''):
    errorIndication, errorStatus, errorIndex, varBinds = next(
        setCmd(SnmpEngine(),            
        UsmUserData('snmpAdmin', 'password', 'password',
                    authProtocol=usmHMACSHAAuthProtocol,
                    privProtocol=usmAesCfb128Protocol),
        UdpTransportTarget((ip, 161),),
        ContextData(contextName=context),
        ObjectType(ObjectIdentity(oid), OctetString(value) ))    
    )    
    values = '  =  '
    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:
            values = ' = '.join([x.prettyPrint() for x in varBind])
            print(values)
    return values.split(' = ')[1]

Console output

OctetString(value) 
<OctetString value object at 0x615ba90 tagSet <TagSet object at 0x502ca70 tags 0:0:4> subtypeSpec <ConstraintsIntersection object at 0x4fef090 consts <ValueSizeConstraint object at 0x4fe0b70 consts 0, 65535>> encoding iso-8859-1 payload [2019-04-04,10:27:41.0,+12:0]>

errorStatus
<Integer value object at 0x6106970 tagSet <TagSet object at 0x4ff59b0 tags 0:0:2> namedValues <NamedValues object 0x54d36b0 enums noError=0, tooBig=1, noSuchName=...Writable=17, inconsistentName=18> payload [commitFailed]>

I am assuming that pySnmp will need the same input type as iReasoning but I am running out of clue on how to do it. Thanks!


Solution

  • For a simple solution to the date/time octet string only, I did the following: I have to use strut pack to convert the date-time string into bytes. something like this, struct.pack('>HBBBBBBcBB', 'yyyy', 'mm','dd','H','M','S',0,b'+',time_offset,0) which will be b'\x07\xe3\x04\x04\x0b\x02!\x07+\r\x00' and can be sent to the above snmpV3string function