vlan/mac/port from switch fdb

#!/bin/bash
 
HOST=$1
PORTPARAM=$2
 
if [[ -n "$PORTPARAM" && ( "$PORTPARAM" -lt 0 || "$PORTPARAM" -gt 24 ) ]]; then
    echo "Error port number!"
    exit
fi
 
for string in `snmpwalk -v 2c -c public $HOST 1.3.6.1.2.1.17.7.1.2.2.1.2 -O qn|sed -e "s/^\.//g" -e "s/ /./g"`; do
    decmac=$(echo $string|awk -F "." '{print $15";"$16";"$17";"$18";"$19";"$20}')
    vlan=$(echo $string|awk -F "." '{print $14 }')
    mac=""
    for hex in `echo "obase=16; $decmac"|bc`; do
    if [ ${#hex} == "1" ]; then
        hex="0"$hex
    fi
    if [ -z $mac ]; then
        mac=$hex
    else
        mac=$mac":"$hex
    fi
    done
    port=$(echo $string|awk -F "." '{print $21}')
    if [ -z "$PORTPARAM" ]; then
    printf $vlan"\t"$mac"\t"$port"\n"
    elif [ $PORTPARAM == $port ]; then
    printf $vlan"\t"$mac"\t"$port"\n"
    fi
done

Leave a Reply

Your email address will not be published. Required fields are marked *