IPv4=$( ifconfig |grep -v 'eth1:' |grep -A 1 'eth1'| tail -1 |cut -d ':' -f 2 |cut -d ' ' -f 1)
IPnode1=$"111.22.333.44"
IPnode2=$"111.22.333.45"
ifconfig |grep -v 'eth1:' |grep -A 1 'eth1'| tail -1 |cut -d ':' -f 2 |cut -d ' ' -f 1
if [[ "$IPv4" = "$IPnode1" ]]; then
echo "found the address "
echo "111.22.333.44 VM01.com VM01" >> /etc/hosts
else
echo "The address does not match"
fi
ifconfig |grep -v 'eth1:' |grep -A 1 'eth1'| tail -1 |cut -d ':' -f 2 |cut -d ' ' -f 1
if [[ "$IPv4" = "$IPnode2" ]]; then
echo ""
echo "found the address "
echo "111.22.333.45 VM02.com VM02" >> /etc/hosts
else
echo "The address does not match"
fi
Asked
Active
Viewed 47 times
-1

melpomene
- 84,125
- 8
- 85
- 148

user3112614
- 1
- 3
-
What's in $IPv4 after executing the first line? – Hellmar Becker Nov 17 '18 at 17:05
-
BTW, `ifconfig` on Linux is deprecated and has been unmaintained (except by distros adding their own local patches) for over a decade; it's completely unaware of modern Linux networking features (like named aliases), and consequently can misrepresent advanced configurations' status. New code should use the `ip` tool from `iproute2` instead; this also gives you a single-line-per-interface output option so you don't need hackery equivalent to the `grep -A 1 | tail` done here. – Charles Duffy Nov 17 '18 at 20:16
-
More to-the-point, `PS4=':$LINENO+' bash -x yourscript` will log each line as it's run, making it easier to follow what's going on with logic errors. (One caveat is that when running as root, modern versions of bash don't allow `PS4` to be inherited via the environment for security reasons, so you need to put that assignment in your code to get the logs to include line number). – Charles Duffy Nov 17 '18 at 20:19
1 Answers
0
Your code will always report "The address does not match". If the address matches $IPnode1
, then it doesn't match $IPnode2
, and vice versa. You should only execute the second test when the first one fails.
if [[ "$IPv4" = "$IPnode1" ]]; then
echo "found the address "
echo "$IPnode1 VM01.com VM01" >> /etc/hosts
elif [[ "$IPv4" = "$IPnode2" ]]; then
echo "found the address "
echo "$IPnode2 VM02.com VM02" >> /etc/hosts
else
echo "The address does not match"
fi

Barmar
- 741,623
- 53
- 500
- 612
-
-
You are correct about the result I am getting "The address does not match". – user3112614 Nov 19 '18 at 18:22
-
I think it should work the way I rewrote it. What does `echo $IPv4` show? – Barmar Nov 19 '18 at 21:13
-
echo "$IPnode1 VM02.com VM02" >> /etc/hosts please let me know if ($IPnode1) is correct or should that be ($IPnode2) ? – user3112614 Nov 20 '18 at 16:45
-
-
This is a cluster system I need to catch the active interface when and add the entry in the /etc/hosts .... – user3112614 Nov 20 '18 at 16:49
-
What's the problem? If the active interface address is `$IPnode1` it will add the `VM01` line, if the active interface address is `$IPnode2` it will add the `VM02` line. – Barmar Nov 20 '18 at 16:51
-
I just tested that it does not add the $IPnode2 when it is on VM02 – user3112614 Nov 20 '18 at 17:59
-
-
-
The best way to debug a shell script is to put `set -x` at the beginning. It will then show every line as it's being executed, with the variables expanded. – Barmar Nov 20 '18 at 18:02
-
$IPV4 gets the correct interface according to the VM it is pointing to . – user3112614 Nov 20 '18 at 18:23
-
it does get me the correct interface of $IPnode2 if I run the ifconfig COMMAND – user3112614 Nov 20 '18 at 18:25
-
so what I am trying to do is when I am on VM01 and the ifconfig got the correct interface it will add the entry in /etc/hosts ..So this is a cluster primary / Failover so when the failover becomes the primary it will be VM02 and that needs to be in the /etc/hosts – user3112614 Nov 20 '18 at 18:30
-
I don't really understand. Is the address of `eth1` changing when failover happens? Do you re-run the script when that happens? – Barmar Nov 20 '18 at 19:36
-
Why don't you just put both `VM01` and `VM02` in `/etc/hosts` all the time. – Barmar Nov 20 '18 at 19:36