2011-12-22 37 views
10

¿Cómo obtener valor mac y vlan de la tabla fdb usa python?
En bash trabajo fino snmpwalk: ¿Cómo obtener datos de SNMP con python?

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 

salida: [(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() 

salida: ('27', '27', '25', '27', '27', '27', '24', '27', '25', '18', '4', '27', '25', '27', '27', '25', '27', '27', '27', '27', '27', '27', '27', ' 27 ',' 27 ',' 27 ',' 27 ',' 27 ',' 27 ',' 27 ',' 27 ',' 27 ',' 23 ', ' 25 ',' 27 ', '27 ',' 27 ',' 25 ',' 27 ',' 25 ',' 27 ',' 27 ',' 25 ',' 27 ', ' 27 ',' 27 ',' 27 ',' 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', ' 25, 27, 20, 19, 27, 27, 27, 27, 27, 27 ',' 27 ',' 27 ',' 27 ',' 27 ', ' 27 ',' 11 ',' 25 ',' 27 ',' 27 ',' 27 ',' 27 ',' 27 ' , '27', '27', '27', '27', '27', '27', '27', '27', '27', '25', '27', '27', '27', '2 7 ', ' 27 ',' 27 ',' 27 ',' 27 ',' 27 ',' 2 ',' 27 ',' 5 ',' 27 ',' 0 ',' 27 ', '27 ', '27', '27', '27')

escritura de abetos (pysnmp) volver noSuchInstance. El segundo script (netsnmp) devuelve la lista de puertos pero sin mac y vlan. ¿Qué equivocado?

Respuesta

8

En el ejemplo de pysnmp, usted está haciendo un SNMPGET (snmpget), no un GETNEXT (snmpwalk). Si cambia,

real_fun = getattr(generator, 'getCmd') 

a

real_fun = getattr(generator, 'nextCmd') 

usted comenzará a ver resultados útiles.

En cuanto a la discrepancia que viste en los resultados entre snmpwalk y los enlaces Python net-snmp Resultado snmpwalk y snmpbulkget se comportan de manera diferente. Si haces un snmpbulkget desde la línea de comando con las mismas opciones que el snmpwalk, recibirás los mismos resultados que tu ejemplo de python net-snmp.

Si actualiza la siguiente línea en el ejemplo pitón net-snmp,

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

a

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

entonces ahora debe conseguir la misma lista de los resultados del ejemplo pitón net-snmp como se ve cuando haces un snmpwalk en la línea de comando.