martes, 23 de febrero de 2010

Realizar un ping en un equipo Cisco con SNMP. Script en bash

Objetivo:
Desde un equipo con Linux indicarle a un Routero LAN Switch Cisco que realice un ping a un destino

Motivo:
Pueden existir diversos motivos para realizar la tarea mencionada. Por ejemplo en este momento necesito revisar si en la tabla arp de un equipo se encuentra una MAC en especifico y no tengo acceso desde mi NMS.

Software necesario:
net-snmp
snmp-devel

Configuración del router Cisco:
Para llevar a cabo dicha tarea es necesario tener acceso RW vía SNMP al router. Por ejemplo en modo configuracion:
#snmp-server community acostanetwork RW

Script en Linux:
#!/bin/sh
###### We've chosen 333 at random. 333 will be the row instance to use for this particular
###### ping experiment. After the ping, the row will be deleted.
###### This keeps the table clean. Router_Source is the dns name of the device we are
###### working with, and public is its RW community string. The values for
###### ciscoPingEntryStatus status are as follows (see Ping MIB):

###### 1 - active
###### 2 - notInService
###### 3 - notReady
###### 4 - createAndGo
###### 5 - createAndWait
###### 6 - destroy

#DECLARACION DE LAS VARIABLES
COM="epale" #SNMP Community name
PINGER_ROUTER="10.3.4.5"
IP_TO_PING="0A 00 00 01" #The IP address to ping should be define in HEX
PACKET_COUNT=20
PACKET_SIZE=100

###### We will clear out any previous entries by setting ciscoPingEntryStatus = 6 (destroy)

snmpset -c $COM $PINGER_ROUTER .1.3.6.1.4.1.9.9.16.1.1.1.16.333 integer 6
###### We start building the row by setting ciscoPingEntryStatus = 5 (createAndWait)
echo

snmpset -c $COM $PINGER_ROUTER .1.3.6.1.4.1.9.9.16.1.1.1.16.333 integer 5

echo
echo "###### Now let's set the characteristics of the ping #######"

###### Only the first three sets below are REQUIRED. The rest have default
###### values.

#Set ciscoPingEntryOwner = any_name
snmpset -c $COM $PINGER_ROUTER .1.3.6.1.4.1.9.9.16.1.1.1.15.333 s anyname

#Set ciscoPingProtocol = 1 = ip (see CISCO-TC-V1SMI.my CiscoNetworkProtocol)
snmpset -c $COM $PINGER_ROUTER .1.3.6.1.4.1.9.9.16.1.1.1.2.333 i 1

#Set ciscoPingAddress = #.#.#.#--take Remote_Dest's ip & convert each octet to hex
snmpset -c $COM $PINGER_ROUTER .1.3.6.1.4.1.9.9.16.1.1.1.3.333 x "$IP_TO_PING"
#Set the packet count to 20 (ciscoPingPacketCount)
snmpset -c $COM $PINGER_ROUTER .1.3.6.1.4.1.9.9.16.1.1.1.4.333 i $PACKET_COUNT

#Set the packetsize to 100 (ciscoPingPacketSize)
snmpset -c $COM $PINGER_ROUTER .1.3.6.1.4.1.9.9.16.1.1.1.5.333 i $PACKET_SIZ

echo
echo "##### Now let's verify that the ping is ready to go and launch it #######"

#Get ciscoPingEntryStatus and make sure it is now equal to 2. This means
# notInService which indicates that we're ready to go.

snmpget -c $COM $PINGER_ROUTER .1.3.6.1.4.1.9.9.16.1.1.1.16.333

# Set ciscoPingEntryStatus = 1 to tell it to activate.

snmpset -c $COM $PINGER_ROUTER .1.3.6.1.4.1.9.9.16.1.1.1.16.333 integer 1

#Let's wait two seconds before looking the results
sleep 2

echo
echo "##### Let's look at the results. #####"

snmpwalk -c $COM $PINGER_ROUTER .1.3.6.1.4.1.9.9.16.1.1.1

echo

echo "##### Now that we've gotten the results, let's destroy the row #####"
snmpset -c $COM $PINGER_ROUTER .1.3.6.1.4.1.9.9.16.1.1.1.16.333 integer 6



Funcionamiento:
- Declarar las variables de manera correcta al comienzo del script
- Notese que existe un sleep en el script. Este sleep sirve para esperar 2 segundos antes de continuar a ver los resultados. Es importante debido a que si se ejecuta el script sin la pausa el router probablemente no tendrá tiempo de tener los resultados.
- Recomiendo (pero no es necesario) instalar los MIBs ubicados en: http://tools.cisco.com/Support/SNMP/do/BrowseMIB.do?local=en&mibName=CISCO-PING-MIB

Ejemplo de resultado (con MIBs compilados):
CISCO-PING-MIB::ciscoPingProtocol.333 1
CISCO-PING-MIB::ciscoPingAddress.333 "0A 00 00 01 "
CISCO-PING-MIB::ciscoPingPacketCount.333 20
CISCO-PING-MIB::ciscoPingPacketSize.333 100
CISCO-PING-MIB::ciscoPingPacketTimeout.333 2000
CISCO-PING-MIB::ciscoPingDelay.333 0
CISCO-PING-MIB::ciscoPingTrapOnCompletion.333 2
CISCO-PING-MIB::ciscoPingSentPackets.333 20
CISCO-PING-MIB::ciscoPingReceivedPackets.333 20
CISCO-PING-MIB::ciscoPingMinRtt.333 1
CISCO-PING-MIB::ciscoPingAvgRtt.333 1
CISCO-PING-MIB::ciscoPingMaxRtt.333 1
CISCO-PING-MIB::ciscoPingCompleted.333 1
CISCO-PING-MIB::ciscoPingEntryOwner.333 "anyname"
CISCO-PING-MIB::ciscoPingEntryStatus.333 1
CISCO-PING-MIB::ciscoPingVrfName.333 ""

He colocado en negrillas e itálicas algunos valores importantes

Link importantes:


Espero sea de tu utilidad, suerte,

No hay comentarios:

Publicar un comentario

¿Algo adicional que quieras mencionar? ¿Algun consejo?, ¿truco? Gracias!

BGP Stream: un año de análisis sobre incidentes BGP

BGP Stream: un año de análisis sobre incidentes BGP 04/03/2024 Por  Alejandro Acosta , Coordinador de I+D en LACNIC LACNIC presenta  la prim...