文章标题

来源:互联网 发布:女权五姐妹事件知乎 编辑:程序博客网 时间:2024/05/24 05:34

从虚拟机模板部署虚拟机,快速解决网卡配置

#!/bin/bash#此脚本实现从ESXI新开模板之后,快速配置机器网络及MAC地址,涉及两个文件的更改#定义常量net_rule_file="/etc/udev/rules.d/70-persistent-net.rules"net_conf_file="/etc/sysconfig/network-scripts/ifcfg-eth0"netmask_conf="255.255.xxx.0"gateway_conf="192.168.xxx.xx"dns1_conf="114.xxx.xxx.114"old_mac="00:50:56:xx:F6:xx"#判断是否已正确执行过脚本,然后从文件中删除旧的MAC,更改为新的MAC地址,--eth0if  (cat $net_rule_file|grep -i $old_mac) ;then  new_mac_str=$(sed -n -e '/eth1/ p' $net_rule_file)  #new_mac_1=${new_mac_str:64:17}  new_mac=$(echo $new_mac_str| awk -F ',' {'print $4'}|awk -F '==' {'print $2'}|sed 's/\"//g')  sed -i "/$old_mac/Id" $net_rule_file  sed -i "s/eth1/eth0/g" $net_rule_fileelse  new_mac_str=$(sed -n -e '/eth0/ p' $net_rule_file)  #new_mac_1=${new_mac_str:64:17}  new_mac=$(echo $new_mac_str| awk -F ',' {'print $4'}|awk -F '==' {'print $2'}|sed 's/\"//g')  echo "done 70-persistent-net.rules file!"fi#===================================#将新的网络配置入写网卡文件,重启网络if  (cat $net_conf_file|grep $netmask_conf) ;then  echo "done /etc/sysconfig/network-scripts/ifcfg-eth0"elif [ ! -n "$1" ] ;then    echo "you have not input a ip address!"else  sed -i "/$old_mac/Id" $net_conf_file  sed -i "s/dhcp/static/g" $net_conf_file  echo "HWADDR=$new_mac" >> $net_conf_file  echo "IPADDR=$1" >> $net_conf_file  echo "NETMASK=$netmask_conf" >> $net_conf_file  echo "GATEWAY=$gateway_conf" >> $net_conf_file  echo "DNS1=$dns1_conf" >> $net_conf_file  service network restartfi
0 0
原创粉丝点击