1
我執行使用PySNMP作爲SNMP代理表中實現一個表。在PySNMP劑
我已經按照使用自動生成的TRS-MIB.py文件的(PySNMP源)架構,並且包含執行一個手動編寫__TRS-MIB.py文件。
我已經做了工作(2行)表,但代碼是十分可怕的,所以必須有這樣做的更好的方法。 (此外,兩行總會有相同的價值觀,這是一個錯誤。)
__TRS-MIB.py:
# Imported just in case new ASN.1 types would be created
from pyasn1.type import constraint, namedval
import time
# Imports
(Integer,
ObjectIdentifier,
OctetString,) = mibBuilder.importSymbols(
"ASN1",
"Integer",
"ObjectIdentifier",
"OctetString"
)
(Bits,
Integer32,
ModuleIdentity,
MibIdentifier,
MibScalar,
MibScalarInstance,
TimeTicks,) = mibBuilder.importSymbols(
"SNMPv2-SMI",
"Bits",
"Integer32",
"ModuleIdentity",
"MibIdentifier",
"MibScalar",
"MibScalarInstance",
"TimeTicks"
)
class TrsDeliveryTime(Integer32):
def clone(self, **kwargs):
if 'value' not in kwargs:
kwargs['value'] = int(time.time())
return Integer32.clone(self, **kwargs)
class TrsMessagesPerHour(Integer32):
def clone(self, **kwargs):
if 'value' not in kwargs:
kwargs['value'] = -int(time.time())
return Integer32.clone(self, **kwargs)
class TrsGatewayIndex(Integer32):
def clone(self, **kwargs):
if 'value' not in kwargs:
kwargs['value'] = 0
return Integer32.clone(self, **kwargs)
class TrsGatewayName(OctetString):
def clone(self, **kwargs):
if 'value' not in kwargs:
kwargs['value'] = 'SMG0'
return OctetString.clone(self, **kwargs)
class TrsGatewayState(OctetString):
def clone(self, **kwargs):
if 'value' not in kwargs:
kwargs['value'] = 'UP' + str(int(time.time()))
return OctetString.clone(self, **kwargs)
# Objects
(trsDeliveryTime,
trsMessagesPerHour,
trsGatewayTable,
trsGatewayEntry,
trsGatewayIndex,
trsGatewayName,
trsGatewayState,) = mibBuilder.importSymbols(
'TRS-MIB',
'trsDeliveryTime',
'trsMessagesPerHour',
'trsGatewayTable',
'trsGatewayEntry',
'trsGatewayIndex',
'trsGatewayName',
'trsGatewayState',
)
__trsDeliveryTime = MibScalarInstance(trsDeliveryTime.name, (0,), TrsDeliveryTime(0))
__trsMessagesPerHour = MibScalarInstance(trsMessagesPerHour.name, (0,), TrsMessagesPerHour(0))
__trsGatewayIndex0 = MibScalarInstance(trsGatewayIndex.name, (0,), TrsGatewayIndex(0))
__trsGatewayName0 = MibScalarInstance(trsGatewayName.name, (0,), TrsGatewayName(0))
__trsGatewayState0 = MibScalarInstance(trsGatewayState.name, (0,), TrsGatewayState(0))
__trsGatewayIndex1 = MibScalarInstance(trsGatewayIndex.name, (1,), TrsGatewayIndex(0))
__trsGatewayName1 = MibScalarInstance(trsGatewayName.name, (1,), TrsGatewayName(0))
__trsGatewayState1 = MibScalarInstance(trsGatewayState.name, (1,), TrsGatewayState(0))
# Augmentions
# Exports
# Objects
mibBuilder.exportSymbols("__TRS-MIB",
trsDeliveryTime=__trsDeliveryTime,
trsMessagesPerHour=__trsMessagesPerHour,
trsGatewayTable=trsGatewayTable,
trsGatewayIndex0=__trsGatewayIndex0,
trsGatewayName0=__trsGatewayName0,
trsGatewayState0=__trsGatewayState0,
trsGatewayIndex1=__trsGatewayIndex1,
trsGatewayName1=__trsGatewayName1,
trsGatewayState1=__trsGatewayState1,
)
謝謝,這是非常有用的。當我用這個,我得到一個錯誤'AttributeError的:例如位整數。20在我查詢索引列值的時間沒有__call__ method'。如果我查詢八位組串列值之一,我得到'AttributeError的:OctetString和實例沒有__call__ method'。看起來好像比預期的還要多一個函數調用級別,或者類似。 – fadedbee
P.S.我不得不改變'... getName(),0,trs ...'爲'... getName(),(0,),trs ...'使它運行。 – fadedbee
P.P.S.更改'(0,)'的'0'只會使其運行,查詢時代碼仍會產生屬性錯誤。 – fadedbee