根据MAC生成唯一IP
冷胜魁(Seaquester)
lengshengkui@gmail.com
2009-5-15
由于某种情况下需要根据MAC地址,给网卡设定唯一的IP,所以,就写了下面一个脚本。
mac2ip.sh:
#!/bin/sh
if [ $# -ne 1 ] ; then
echo "Usage: `basename $0` "
exit 1
fi
# We need convert to Uppercase, otherwise the
# bc will reutrn error
MAC=`echo $1 | tr [:lower:] [:upper:]`
ip1=`echo $MAC | awk -F':' '{print $4}'`
ip2=`echo $MAC | awk -F':' '{print $5}'`
ip3=`echo $MAC | awk -F':' '{print $6}'`
ip1=`echo "ibase=16;$ip1"|bc`
ip2=`echo "ibase=16;$ip2"|bc`
ip3=`echo "ibase=16;$ip3"|bc`
echo "$ip1.$ip2.$ip3"
exit 0
mac2ip.sh的使用方式如下:
#!/bin/sh
#!/bin/sh
MAC=`ifconfig eth0 | grep eth0 | awk '{print $5}'`
if [ x"$MAC" == "x" ] ; then
echo "Invalid MAC address!"
exit 1
fi
MGMT_IP=`./mac2ip.sh ${MAC}`
MGMT_IP=`echo 10.${MGMT_IP}`
echo $MGMT_IP
阅读(1846) | 评论(0) | 转发(0) |