我正在構(gòu)建一個(gè)腳本,該腳本應(yīng)該通過SNMP(漫游)從調(diào)制解調(diào)器獲取MAC / IP地址。為此,我正在使用PySNMP和nextCmd(asyncore)。盡管我得到了想要的東西,但是目前我得到的比預(yù)期的要多:在特定節(jié)點(diǎn)上行走之后,它會與剩下的所有其他節(jié)點(diǎn)一起繼續(xù)工作。代碼: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貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超9個(gè)贊
缺少解決此問題的任何內(nèi)置方法,我對進(jìn)行了更改cbFun()
?,F(xiàn)在,它包含一些額外的行,re.search()
并且pattern是OID(因此,如果pattern不匹配,即nextCmd轉(zhuǎn)到另一個(gè)級別(節(jié)點(diǎn)),它就可以了return
)。我已經(jīng)很簡單了,因?yàn)橐呀?jīng)構(gòu)造了帶有response的字符串print(' = '.join([x.prettyPrint() for x in varBind]))
,所以我已經(jīng)添加了if
條件,并將上面代碼的結(jié)果分配給了變量,因此使實(shí)現(xiàn)此檢查成為可能。
作為另一種可能的解決方案,也可以使用本示例中描述的方法-使用varBindHead并進(jìn)行比較。
隨時(shí)添加您的解決方案。
添加回答
舉報(bào)
0/150
提交
取消