Skip to content

Instantly share code, notes, and snippets.

@nori-nori
Created November 18, 2014 05:30
Show Gist options
  • Save nori-nori/f1682de1848a19e84b88 to your computer and use it in GitHub Desktop.
Save nori-nori/f1682de1848a19e84b88 to your computer and use it in GitHub Desktop.
script for updating Mac Addresses on cloned VM of RHEL/CentOS6
#!/bin/sh
UdevRules=/etc/udev/rules.d/70-persistent-net.rules
mv -f ${UdevRules} ${UdevRules}.old
udevadm trigger --subsystem-match="net" --action add
sleep 5
if [ ! -e "${UdevRules}" ]; then
exit 1
fi
for devFile in /etc/sysconfig/network-scripts/ifcfg-eth*
do
# main
devName=${devFile##*-}
MacAddr=`grep ${devName} ${UdevRules} | awk '{print $4}' | sed -e 's/.*"\(.*\)",.*/\1/;'`
grep HWADDR "${devFile}" >/dev/null 2>&1
if [ $? -eq 0 ]; then
sed -e "s/^HWADDR.*/HWADDR=${MacAddr}/" ${devFile} > /tmp/ifcfg.$$
cp /tmp/ifcfg.$$ ${devFile}
rm -f /tmp/ifcfg.$$
else
printf "HWADDR=${MacAddr}\n" >> ${devFile}
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment