#!/bin/bash
# $id$

#INPUT: when $1 is arp will show arp
#            $1 is neigh will show neighor
#            $1 is either eth0 or eth1 will show interface information
#            $2 can specify an interface name
#used for CLI command "show management-ethernet interface info/arp/neighbor"
#

print_device()
{
    ip addr show dev $1 > /dev/null 2>&1 ||  { echo >&2 ; return; } 
    printf "%s" $1
    ip addr show dev $1 | grep "link/ether" | awk '{printf "\tHwaddr: %s",$2}'
    ip addr show dev $1 | grep "state" | awk '{printf "\tState: %s\n",$9}'
    ip -4 route | grep $1 | awk '/default/{printf "\tGateway  : %s\n",$3}' 
    printf "\tInet addr: \n"
    ip addr show dev $1 | awk '$1=="inet" {printf "\t           %s\n",$2}'
    ip addr show dev $1 | awk '$1=="inet6" {printf "\t           %s\n",$2}'
    printf "    Traffic statistics\n"
    ip -s link show dev $1 | awk 'NR==4 {printf "\tInput Packets......................%s\n\tInput Bytes........................%s\n", $2,$1}' 
    ip -s link show dev $1 | awk 'NR==6 {printf "\tOutput Packets.....................%s\n\tOutput Bytes.......................%s\n", $2,$1}' 
    printf "\n"
}

print_arp()
{
    echo "Address          HW Address         Interface";
    echo "---------------  -----------------  ---------";
    while [ -n "$1" ]; do
        ip link show dev $1 > /dev/null 2>&1 ||  { echo >&2 ; return; } 
        ip -4 neigh show dev $1 | awk -v dev=$1 '{printf "%-17s%-19s%-11s\n",$1,$3,dev}'
        shift;
    done
}

print_neigh()
{
    echo "Address                                  HW Address        Interface";
    echo "---------------------------------------  ----------------- --------- ";
    while [ -n "$1" ]; do
        ip link show dev $1 > /dev/null 2>&1 ||  { echo >&2 ; return; } 
        ip -6 neigh show dev $1 | awk -v dev=$1 '{printf "%-41s%-19s%-11s\n",$1,$3,dev}' 
        shift;
    done
}

if [ "$1" = "arp" ]; then
    if [ "$2" = "eth0" ] || [ "$2" = "eth1" ]; then
        print_arp $2
    else
        print_arp eth0 eth1
    fi
elif [ "$1" = "neigh" ]; then
    if [ "$2" = "eth0" ] || [ "$2" = "eth1" ]; then
        print_neigh $2
    else
        print_neigh eth0 eth1
    fi
elif [ "$1" = "eth0" ] || [ "$1" = "eth1" ]; then
    print_device $1
else 
    print_device eth0
    print_device eth1
fi

exit 0