#!/bin/sh
#
# Author xiaosuo # Licence GPL2 or above
# Check if the network card is mapped correctly
#
CONFFILE=/etc/conf.d/ifmap.conf
if [ ! -e $CONFFILE ]; then
echo "config file:$CONFFILE isn't found"
exit
fi
while read line; do
eth_name="";
eth_addr="";
# skip the comment
if echo $line | grep -q -e "^#.*$" ; then
continue;
fi
eth_name=`echo $line | awk '{print $1}'`
eth_addr=`echo $line | awk '{print $2}' | tr "[:upper:]" "[:lower:]"`
if ip link show dev $eth_name &>/dev/null; then
real_addr=`ip link show dev $eth_name | grep 'link/ether' | awk '{print $2}'`
if [ $real_addr = $eth_addr ]; then
continue;
fi
fi
# search for the correct link address
echo "$eth_name($eth_addr) is changed"
got="NO"
for (( i=0; i < 256 ; i ++ )) ; do
search_name="eth$i"
if ip link show dev $search_name &>/dev/null; then
search_addr=`ip link show dev $search_name | grep 'link/ether' | awk '{print $2}'`
if [ $search_addr = $eth_addr ]; then
got="YES"
break;
fi
fi
done
if [ $got = "NO" ]; then
echo "Cann't config $eth_name"
continue;
fi
# swap the dev name
echo substitute $eth_name for $search_name
ip link set dev $eth_name down &>/dev/null
ip link set dev $search_name down &>/dev/null
ip link set dev $eth_name name eth_tmp &>/dev/null
ip link set dev $search_name name $eth_name &>/dev/null
ip link set dev eth_tmp name $search_name &>/dev/null
ip link set dev $eth_name up &>/dev/null
ip link set dev $search_name up &>/dev/null
/etc/init.d/net.$eth_name restart
/etc/init.d/net.$search_name restart
done < $CONFFILE