A continuación se muestra un script en shell que obtiene el estatus de los puertos trunk de un switch Cisco y los muestra en pantalla, si no puede obtener los valores mostrará el mensaje de error.
Los valores son almacenados en archivos ya que es mas rápido operar éstos que realizar varias consultas snmp al equipo.
#VARIABLES GLOBALES ip_equipo=$1 communi="TU_COMUNIDAD" RUTA="/RUTA/DEL/SCRIPT" function obtieneTrunkDown { ipe=$1 ##OBTENEMOS VALORES DE LAS INTERFACES /opt/OV/bin/snmpwalk -c $communi $ipe 1.3.6.1.4.1.9.9.46.1.6.1.1.14 | awk 'BEGIN { FS=":" } {print $1, $3}'| tr -s " " ":" > $RUTA/interfaces.$ipe /opt/OV/bin/snmpwalk -c $communi $ipe 1.3.6.1.2.1.2.2.1.2 | awk 'BEGIN { FS=":" } {print $1, $3}' | tr -s " " ":" > $RUTA/idesc.$ipe /opt/OV/bin/snmpwalk -c $communi $ipe 1.3.6.1.2.1.2.2.1.8 | awk 'BEGIN { FS=":" } {print $1, $3}' | tr -s " " ":" > $RUTA/istatus.$ipe #DETERMINAMOS TRUNKS awk 'BEGIN { FS=":" }{if ($2==1) print $1}' $RUTA/interfaces.$ipe | cut -d"." -f9 > $RUTA/indicesTrunk.$ipe && rm $RUTA/interfaces.$ipe #OBTENEMOS DESCRIPCION Y ESTATUS DE LOS TRUNKS for indices in `cat $RUTA/indicesTrunk.$ipe` do grep "ifDescr.$indices:" $RUTA/idesc.$ipe | awk 'BEGIN { FS=":" } {print $2}' >> $RUTA/descfinal.$ipe grep "ifOperStatus.$indices:" $RUTA/istatus.$ipe | awk 'BEGIN { FS=":" } {print $2}' >> $RUTA/operfinal.$ipe done rm $RUTA/idesc.$ipe $RUTA/istatus.$ipe paste $RUTA/descfinal.$ipe $RUTA/operfinal.$ipe > $RUTA/stattrunk.$ipe rm $RUTA/descfinal.$ipe $RUTA/operfinal.$ipe $RUTA/indicesTrunk.$ipe if test -s $RUTA/stattrunk.$ipe then awk 'BEGIN { print " Interfaces Status " print "============== =========="} { printf "% -20s %s\n", $1, $2 }' $RUTA/stattrunk.$ipe else echo "Error al obtener valores" fi rm $RUTA/stattrunk.$ipe } #////////MAIN/////////// if test ! -z $1 then obtieneTrunkDown $ip_equipo else echo echo "ERROR no se tecleo la ip del equipo" echo "Utiliza sh <script> <IP>" echo fi
Para ejecutar el script tenemos
#>sh muestraTrunk 10.183.104.155 2>/dev/null
Interfaces Status
============== ==========
FastEthernet0/1 up
FastEthernet0/2 up
FastEthernet0/3 up
FastEthernet0/20 up
FastEthernet0/21 up
FastEthernet0/22 up
FastEthernet0/23 up
FastEthernet0/24 up
#>sh muestraTrunk 10.183.104.15 2>/dev/null
Error al obtener valores
#>sh muestraTrunk 2>/dev/null
ERROR no se tecleo la ip del equipo
Utiliza sh <script> <IP>
El script se programo sobre un equipo HP-UX
😛