0

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!

DonAriston
  • 101
  • 1
  • 5
  • At packet level, raw bytes are sent, not the date-time string. You have to do some conversion according to the RFC document. – Lex Li Apr 04 '19 at 00:02
  • thanks I just figured out 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 – DonAriston Apr 04 '19 at 00:21
  • then post your own answer and accept it. – Lex Li Apr 04 '19 at 00:21
  • okay. It will take 48hrs to answer my own question. – DonAriston Apr 04 '19 at 03:22

2 Answers2

0

You have to get pysnmp taking the time stamp in human-friendly form and turn it into the series of octets. The format information is contained in the SNMPv2-TC::DateAndTime TEXTUAL-CONVENTION:

DISPLAY-HINT "2d-1d-1d,1d:1d:1d.1d,1a1d:1d"

For pysnmp to use this you can pass MIB object name (HOST-RESOURCES-MIB::hrSystemDate) instead of OID when preparing SNMP command:

...
ObjectType(ObjectIdentity('HOST-RESOURCES-MIB', 'hrSystemDate'), value)

It works similarly, but in reverse, for SNMP GET.

Ilya Etingof
  • 5,440
  • 1
  • 17
  • 21
0

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

DonAriston
  • 101
  • 1
  • 5