#!/bin/bash #################### #Author:jncheng # #Vsersion:20150402 # #################### source /etc/profile ###################################################################################################### # #List the system eth‘s Mac-address ###################################################################################################### echo "This MAC-Address is in file /etc/udev/rules.d/70-persistent-net.rules" i=0 while [ $i -lt 5 ] do grep eth$i /etc/udev/rules.d/70-persistent-net.rules > /root/mac.txt if [ $? = 0 ]; then HWADDR2=$(awk ‘{print $4}‘ /root/mac.txt |awk -F == ‘{print $2}‘ |awk -F \" ‘{print $2}‘) ETHNUM=$(awk ‘{print $7}‘ mac.txt |awk -F = ‘{print $2}‘ |awk -F \" ‘{print $2}‘) echo "$ETHNUM MAC is $HWADDR2" else echo "Can‘t find other eth" break fi i=$[i+1] done echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" echo "" ###这里将模板宿主机的物理地址显示出来,告诉这个MAC地址是不可用的。因为通过虚拟主机克隆模板时,宿主机上之前的网卡MAC地址是会保存在模板中的。根据真实的模板中的MAC地址修改吧。 echo "MAC-Address 00:50:56:ae:80:b8 Can‘t Use!!!!!!!!!!!!!!!!!!!!" echo "" ######################################################################################################### #Modify the IP and MAC addresss # ######################################################################################################### read -p "Please input which eth number you will modify(0-4):" input_ethnum grep -vi 00:50:56:AE:80:B8 /etc/udev/rules.d/70-persistent-net.rules |grep eth$input_ethnum > /root/mac.txt ethnumber=$(awk ‘{print $7}‘ mac.txt |awk -F = ‘{print $2}‘ |awk -F \" ‘{print $2}‘) read -p "Please Input IP:" IP read -p "Please Input Gateway:" GATE read -p "Please Input Prefix:" PRE cd /etc/sysconfig/network-scripts if [ -e ifcfg-eth0 ]; then grep -v ^IPADDR ifcfg-eth0 |grep -v ^ONBOOT|grep -v ^NAME |grep -v ^DEVICE |grep -v ^PREFIX |grep -v ^GATEWAY |grep -v ^HWADDR > /root/ifcfg.txt else grep -v ^IPADDR ifcfg-eth1 |grep -v ^ONBOOT|grep -v ^NAME |grep -v ^DEVICE |grep -v ^PREFIX |grep -v ^GATEWAY |grep -v ^HWADDR > /root/ifcfg.txt fi sed -i ‘3a\GATEWAY=‘$GATE‘‘ /root/ifcfg.txt sed -i ‘3a\PREFIX=‘$PRE‘‘ /root/ifcfg.txt sed -i ‘3a\IPADDR=‘$IP‘‘ /root/ifcfg.txt sed -i ‘1a\DEVICE=‘$ethnumber‘‘ /root/ifcfg.txt sed -i ‘1a\ONBOOT=yes‘ /root/ifcfg.txt HWADDR=$(awk ‘{print $4}‘ /root/mac.txt |awk -F == ‘{print $2}‘ |awk -F \" ‘{print $2}‘) sed -i ‘3a\HWADDR=‘$HWADDR‘‘ /root/ifcfg.txt cp ifcfg-$ethnumber ifcfg-$ethnumber.bak cp /root/ifcfg.txt ifcfg-$ethnumber echo "Restart the networking... " service network restart&&echo "Modify is scuefull"||echo "Modify is failed"
本文出自 “什么也没有留下” 博客,请务必保留此出处http://3357278.blog.51cto.com/3347278/1627689
VMware vSphere 通过模板创建虚拟机后修改MAC地址脚本
原文地址:http://3357278.blog.51cto.com/3347278/1627689