#!/bin/bash
# $id$

#INPUT: $1 is either ipv4 or ipv6
#       $2-$n can be any argument of system ping or ping6
#		especially, you can use [-I] to assign a l3 interface
#DESC:
#		this script will use kernel interface name to replace
#		l3 interface name of ping/ping6
#CALLER:
#		CLI ping/ping6
#RETURN:
#		whatever ping/ping6 returns

ver=4
if [ "$1" = "ipv4" ] ; then
	ver=4; shift;
elif [ "$1" = "ipv6" ]; then
	ver=6; shift;
fi

paramstr=`echo $*`
file_dir="/tmp/run/vlan_interfaces"

#check if specified [-I]
while [ -n "$1" ]; do
	case $1 in
    -I) vlan_interface=$2; break;;
    *)  shift ;;
	esac
done

#find real interface name in kernel
if [ -f "$file_dir" ] ; then 
	if [ -n "$vlan_interface" ]; then
		actual_interface=`grep $vlan_interface $file_dir | awk '{print $2}'`
	fi
fi

#use real interface name to replace vlan interface name
if [ -n "$actual_interface" ]; then
	paramstr=`echo $paramstr | sed "s/$vlan_interface/$actual_interface/1"`
fi

if [ "$ver" = 4 ]; then
	ping $paramstr
else
	ping6 $paramstr
fi

exit 0

