The sitch:
We have a hosted virtual environment (I loathe the term 'cloud') on a VMware core which is poorly implemented. It does a bad job of managing RHEL6 copies simply because it was an old version of ESXi that doesn't support it. As a result, we found little need to install VMware Tools. However, we recently became aware that at some point, copies became possible, but not particularly useful since we still don't have VMware Tools installed on any of our RHEL6 VMs (plus, we still don't know how effective it will be even with VMware Tools installed).
This creates a problem with MAC addresses. When a VM is either copied from one compute pool to another or simply moved to a different VLAN, the MAC changes. Since Tools isn't there to manage the behind the scenes reconfiguration during a copy, we are forced do update the MAC settings ourselves in both the udev rules file as well as the ifcfg files. This is tedious and I'd like to write a script that makes the changes for me.
Let's say I have two interfaces: eth0 and eth1. When I make a copy, rather than updating those two interfaces with new MACs, the udev file creates two new entries with the new MACs and names them eth2 and eth4. I then have to go in, either delete or comment out the eth0 and eth1 then edit the two new lines to be eth0 and eth1 instead.
I then have to take those new MACs and enter them into the ifcfg files and then reboot the VM so the networking subsystem associates the new hardware with eth0 and eth1 correctly.
What I have so far:
I can extract each MAC using grep and awk
Code:
# MAC=$(grep eth0 /etc/udev/rules.d/70-persistent-net.rules |awk -F, '{print $4}' |awk -F== '{print $2}')
I can then echo $MAC and get the result I expect:
Code:
# echo $MAC
"00:50:56:88:1c:77"
However, I have to run this individually for each interface assigning to a new variable each time. The quotes are expected and actually needed since RHEL6 now uses them in the ifcfg files.
What I want to do:
For starters, what I want to do is, for each MAC in the file, assign it to a different variable. eth0 to $MAC0, eth1 to $MAC1 etc
I tried using a for loop, but that didn't work:
Code:
# for i in {0..4}; do MAC$i=$(grep eth$i \
/etc/udev/rules.d/70-persistent-net.rules \
|awk -F, '{print $4}' |awk -F== '{print $2}'); done
That results in
Code:
-bash: MAC0="00:50:88:1c:77": command not found
-bash: MAC1="00:50:88:1c:78": command not found
-bash: MAC2="00:50:88:1c:79": command not found
-bash: MAC3="00:50:88:1c:80": command not found
The real questions:
A: a: Why is it trying to execute the variables as if they were commands? b: Does it have to do with the quotes?
B: a: How can I accomplish what I'm trying to do? b: If the quotes are an issue, how should I go about dealing with them?