2011-12-22 14 views
10

fdb 테이블에서 value mac 및 vlan을 얻는 방법은 python을 사용합니까? 떠들썩한 파티 snmpwalk에서 잘 작동에서
: 파이썬으로 SNMP에서 데이터를 가져 오는 방법은 무엇입니까?

snmpwalk -v2c -c pub 192.168.0.100 1.3.6.1.2.1.17.7.1.2.2.1.2 

pysnmp :

import os, sys 
import socket 
import random 
from struct import pack, unpack 
from datetime import datetime as dt 

from pysnmp.entity.rfc3413.oneliner import cmdgen 
from pysnmp.proto.rfc1902 import Integer, IpAddress, OctetString 

ip='192.168.0.100' 
community='pub' 
value=(1,3,6,1,2,1,17,7,1,2,2,1,2) 

generator = cmdgen.CommandGenerator() 
comm_data = cmdgen.CommunityData('server', community, 1) # 1 means version SNMP v2c 
transport = cmdgen.UdpTransportTarget((ip, 161)) 

real_fun = getattr(generator, 'getCmd') 
res = (errorIndication, errorStatus, errorIndex, varBinds)\ 
    = real_fun(comm_data, transport, value) 

if not errorIndication is None or errorStatus is True: 
     print "Error: %s %s %s %s" % res 
else: 
     print "%s" % varBinds 

출력 : (ObjectName의 (1.3.6.1.2.1.17.7.1.2.2.1.2) NoSuchInstance (''))]

import netsnmp 

def getmac(): 
    oid = netsnmp.VarList(netsnmp.Varbind('.1.3.6.1.2.1.17.7.1.2.2.1.2')) 
    res = netsnmp.snmpgetbulk(oid, Version = 2, DestHost='192.168.0.100', 
          Community='pub') 
    return res 

print getmac() 
,

출력 : ('27', '27', '25', '27', '27', '27', '24', '27', '25', '18', '4', 27 ', 27', 27 ', 27', '27', '25', '27', '27', ' 27 ', 27', 27 ', 27', 27 ', 27', 27 ', 23', '25', '27', '27 27, 27, 27, 27, 25, 27, '27', 27 ', 27', 27 ', 27', 25 ', 27' , '27', '27', '27', '27', '25', '27', '27', '27', '27', '27', '27', '27' '27', '27', '27', '27', '27', '25', '25', '25', '7', '27', '27', '9' '27', '27', '27', '27', '27', '27', '27', '20', '19', '27' '27', '27', '27', '27', '27', '27', '27', 27 ', 27', ' '27', '27', '27', '25', '27', '27', '27', '27', '27', ' '27', '2 7 ', '27 ','27 ','27 ','27 ','2 ','27 ','5 ','27 ','0 ','27 ', '27 ', '27', '27 ', '27')

Firs 스크립트 (pysnmp)는 NoSuchInstance를 반환합니다. 두 번째 스크립트 (netsnmp)는 포트 목록을 반환하지만 mac 및 vlan은 반환하지 않습니다. 뭐가 잘못 됐니?

답변

8

pysnmp 예제에서 GETNEXT (snmpwalk)가 아닌 SNMPGET (snmpget)을 수행하고 있습니다. 당신이

real_fun = getattr(generator, 'getCmd') 

real_fun = getattr(generator, 'nextCmd') 

을 변경하는 경우에는 유용한 결과를보고 시작합니다. 당신이 snmpwalk 및 파이썬 net-snmp 바인딩 사이의 결과에서 본 불일치로

가 발생할 : snmpwalksnmpbulkget없이 행동을 다르게. snmpwalk과 동일한 옵션으로 명령 행에서 snmpbulkget을 수행하면 python net-snmp 예제와 동일한 결과가 나타납니다.

당신은 당신이 때 참조로 다음 이제 파이썬 net-snmp 예에서 결과 동일한 목록을 얻어야한다 파이썬 net-snmp 예에 다음 줄,

res = netsnmp.snmpgetbulk(oid, Version=2, DestHost='192.168.0.100', 
          Community='pub') 

res = netsnmp.snmpwalk(oid, Version=2, DestHost='192.168.0.100', 
         Community='pub') 

에를 업데이트하는 경우 명령 행에서 snmpwalk을 수행하십시오.

관련 문제