ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#!/bin/bash
##################################################################
#
# Purpose:
# When RHEL6 VMs are moved between compute pools or VLANs their
# MAC addresses change breaking the interfaces. This script
# updates the ifcfg-eth* and network interface udev rules files
# to make the configuration match the new addresses.
#
# Changelog:
# 1.0 - Initial script - 05/26/2012 - msnyder
# 1.1 - Switched $udevpath to $udevfile incorporating the actual
# file in the variable since it is always the same; added
# documentation - 05/27/2012 - msnyder
# 2.0 - Rewrite to account for variable number of improper
# udev file entries - 05/29/2012 - msnyder
#
##################################################################
#Set paths to file locations
udevfile=/etc/udev/rules.d/70-persistent-net.rules
netpath=/etc/sysconfig/network-scripts
ifaces=$(grep eth $udevfile |wc -l)
shft=$(($ifaces - 1))
adj=$(($ifaces - 3))
for (($i=0; $i<=$shft; $i++))
do
#Extract MAC addresses of old and new udev entries
#These will be used to update the ifcfg-eth* files
MAC[$i]=$(grep eth$i $udevfile |awk -F ',' '{print $4}' |awk -F "==" '{print $2}')
if [ $i -lt 3 ]
then
#If $i is less than 3 comment out the eth$i entry in the udev rules file
sed -i "/eth$i/ s/^/#/" $udevfile
elif [ $i -ge 3 ] && [ $i -lt $adj ]
then
#Delete the lines associated with the old "new" interfaces
sed -i "/eth$i/d"
else
#Adjust the eth field to the correct value
int=$(echo $i |awk -v val=$adj '{print ($1 - $val)}')
sed -i "s/eth$i/eth$int/" $udevfile
#Change the MAC address in the ifcfg-eth* files to match
#the entry in the udev rules file for the new interfaces
sed -i "s/HWADDR.*/HWADDR=${MAC[$i]}/" $netpath/ifcfg-eth$int
fi
done
reboot
The issue is specifically with the line that assigns the output of the awk subtraction to the int variable:
I know that $val is being assigned the value of $adj as is evidenced by the screenshot.* However, the subtraction does not execute and the value of $int is not set properly (for an $adj of 5 I expect an $int of 2).
What am I doing wrong with this line that it isn't subtracting the value properly?
*I had to use a screenshot because the VMware console window doesn't support copying/pasting text.
Last edited by theillien; 05-29-2012 at 06:04 PM.
Reason: The MAC[$i] assignment was missing. This has no affect on the rest of the script during debugging, though.
the problem with that awk is that you do not need $ inside the script:
int=$(echo $i |awk -v val=$adj '{print ($1 - $val)}')
you need to remove it - but of course, (( int = i - adj )) is much better
in line: MAC[$i]=$(grep eth$i $udevfile |awk -F ',' '{print $4}' |awk -F "==" '{print $2}')
only one awk would be enough instead of grep|awk|awk
awk -F, " /eth$i/ "' { split ($4, a, "=="); print a[2] }'
I have not tested because I have no input file, but probably works, after testing: MAC[$i]=$( this is the awk )
if you give a sample file we can try to test it also
_____________________________________
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.
Happy with solution ... mark as SOLVED
(located in the "thread tools")
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.