The Linux script automatically modifies the MAC address in the NIC configuration file.

Source: Internet
Author: User
Tags newmac
The Linux script program automatically modifies the MAC address in the NIC configuration file. when playing with a Linux virtual machine, a virtualHDD installed on the linux system is used to create multiple virtual machines, in this way, you do not need to install the system once every virtual machine is created. After virtualHDD is loaded to the virtual machine ,... the Linux script program automatically modifies the MAC address in the NIC configuration file. when playing with a Linux virtual machine, a virtual HDD installed on the linux system is used to create multiple virtual machines, in this way, you do not need to install the system once every virtual machine is created. After the virtual HDD is loaded to the virtual machine, the MAC address of the new virtual machine will be different from the MAC address in ifcfg-eth [0-9] In the virtual system. after entering the system, the network card number displayed in ifconfig-a is followed by the network card number in the original system. If there are four NICs in the original system, the corresponding numbers are eth0, eth1, eth2, and eth3. then you can use virtual HDD to create a new virtual machine, the new virtual machine is also configured with four NICs. after entering the system, use ifconfig-a to see the following NIC numbers: eth4, eth5, eth6, and eth7. I often encounter such problems. it takes a long time to fix these problems. I have to consider automation issues. The script program below is born. This script mainly modifies the following two files: ifcfg-eth [0-9]: [0-9] is a generic expression. this is a regular expression, so do not understand it. All the MAC addresses in these Nic configuration files must be changed to the MAC address of the NIC on the current machine. 70-persistent-net.rules: determines the ID of the NIC. The content in this file is the network card detected when the system starts. it is automatically generated by the system. This file can be directly cleared. I am here to train my hands, so I have also refined it. After the script is executed, restart the system and everything is OK. We can see the familiar eth0, eth1 ...... And so on. This script is applicable to the RHEL series, CentOS. #! /Bin/bash # update: 2012-12-16 # author: Sean_Gong # version # Modify the ifcfg-eth $ x file based on the number of NICs identified by the system, before modifying the Nic configuration file, check whether the file exists. # If yes, write the MAC addresses of all NICs recognized by the system to the file as comments to facilitate record and check. # When processing 70-persistent-net.rules, you can simply clear the file. # After the system restarts, it adds the identified Nic to the file without manual intervention. # Here I am processing this file to practice sed's ability to process text, exercise program logic, and control the entire program. Cdate = $ (date '+ % Y % m % d') num = $ (ifconfig-a | grep eth | wc-l) for (n = 0; n <$ {num}; n ++) do j = $ (n + 1 )) [-e 70-persistent-net.rules _ $ {cdate}] | cp-fa/etc/udev/rules. d/70-persistent-net.rules 70-persistent-net.rules _ $ {cdate} if [-e/etc/sysconfig/network-scripts/ifcfg-eth $ {n}]; then [-e ifcfg-eth $ {n }_$ {cdate}] | cp-fa/etc/sysconfig/network-scripts/ifcfg-eth $ {n }. /ifcfg-eth $ {n }_$ {cdate} cat ifcfg-eth $ {n }_$ {cdate} | sed '/^ # eth. * $/d'> ifcfg-eth $ {n} _ temp ifconfig-a | grep "HWaddr" | awk '{print "#" $1 ": "$5} '> ifcfg-eth $ {n} _ temp newmac =$ (ifconfig-a | grep" eth "| sort | awk' {print $5} '| nl-n ln | grep "^ $ {j}" | awk '{print $2 }') sed "s/HWADDR. * $/HWADDR = $ newmac/g "ifcfg-eth $ {n} _ temp> ifcfg-eth $ {n} _ vi cp-fa ifcfg-eth $ {n }_ vi/etc/sysconfig/network-scripts/ifcfg-eth $ {n} & rm-f ifcfg-eth $ {n} _ temp grep-I $ {newmac} 70-persistent-net.rules _ $ {cdate} | sed "s/NAME =. * $/NAME = \ "eth $ {n} \"/g "> 70-persistent-net.rules_temp else newmac =$ (ifconfig-a | grep" eth "| sort | awk '{print $5} '| nl-n ln | grep "^ $ {j}" | awk' {print $2 }') grep-I $ {newmac} 70-persistent-net.rules _ $ {cdate} | sed "s/NAME =. * $/NAME = \ "eth $ {n} \"/g "> continuous continue fi done cat 70-persistent-net.rules_temp> encrypt & rm-f 70-persistent-net.rules_temp cp-fa encrypt/etc/ udev/rules. d/70-persistent-net.rules
 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.