我正在構建一個腳本,該腳本應該通過SNMP(漫游)從調制解調器獲取MAC / IP地址。為此,我正在使用PySNMP和nextCmd(asyncore)。盡管我得到了想要的東西,但是目前我得到的比預期的要多:在特定節點上行走之后,它會與剩下的所有其他節點一起繼續工作。代碼:nextCmd(snmpEngine, CommunityData('private'), UdpTransportTarget(('IP.goes.right.here', 161)), ContextData(), ObjectType(ObjectIdentity('1.3.6.1.2.1.4.22.1.2')), cbFun=cbFun)snmpEngine.transportDispatcher.runDispatcher()cbFundef cbFun(snmpEngine, sendRequestHandle, errorIndication, errorStatus, errorIndex, varBindTable, cbCtx):if errorIndication: print(errorIndication) returnelif errorStatus: print('%s at %s' % (errorStatus.prettyPrint(), errorIndex and varBindTable[-1][int(errorIndex) - 1][0] or '?')) returnelse: for varBindRow in varBindTable: for varBind in varBindRow: print(' = '.join([x.prettyPrint() for x in varBind]))return True 這純粹是基于文檔和Internet上的其他一些示例。樣本輸出:1.3.6.1.2.1.4.22.1.2.536870914.some.ip.was.here = 0xMacAddress1.3.6.1.2.1.4.22.1.3.1182728.some.ip.was.here = some.ip.was.here # next node1.3.6.1.2.1.4.22.1.4.1182736.some.ip.was.here = 3 # and even further...因此,這樣做的意圖僅限于步行1.3.6.1.2.1.4.22.1.2。PS我只是從PySNMP開始,我想要類似 snmpwalk -v 2c -c private some.ip.was.here ipNetToPhysicalPhysAddress
2 回答

Cats萌萌
TA貢獻1805條經驗 獲得超9個贊
缺少解決此問題的任何內置方法,我對進行了更改cbFun()
?,F在,它包含一些額外的行,re.search()
并且pattern是OID(因此,如果pattern不匹配,即nextCmd轉到另一個級別(節點),它就可以了return
)。我已經很簡單了,因為已經構造了帶有response的字符串print(' = '.join([x.prettyPrint() for x in varBind]))
,所以我已經添加了if
條件,并將上面代碼的結果分配給了變量,因此使實現此檢查成為可能。
作為另一種可能的解決方案,也可以使用本示例中描述的方法-使用varBindHead并進行比較。
隨時添加您的解決方案。
添加回答
舉報
0/150
提交
取消