LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-22-2016, 07:45 AM   #1
horizn
Member
 
Registered: Jan 2015
Location: UK and Poland
Distribution: Slackware + Debian + Ubuntu
Posts: 170

Rep: Reputation: Disabled
Add one more condition to bash script


Hi,
I am struggling with bash script. The purpose of script is to check whether or not virtual IP (keepalived) is up or not. If it is, then check for rest of external IP addresses configured in /etc/network/interfaces and then bring it up. If virtual IP is down, then check the same, but this time bring down additional IPs if they are still up. Now I need to add another condition: If virtual IP is up, and additional external IP addresses too, then do nothing. What I've done so far is:

Code:
IFACE='eth0'
VIP='1.2.3.4'
IFACES_FILE='/etc/network/interfaces'

STATUS=$( ip address show "$IFACE" | grep -o "$VIP" )

if [ ! -z "$STATUS" ]; then
    ip_addresses=( $( grep -o 'up ip addr add [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*' "$IFACES_FILE" |  grep -o '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*' ) ) 
    for ip in "${ip_addresses[@]}"; do
        ip address add "$ip"/32 dev "$IFACE"
    done
    else
        for ip in "${ip_addresses[@]}"; do
        ip address del "$ip"/32 dev "$IFACE"
    done
fi
 
Old 08-22-2016, 07:18 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Don't know your exact configuration but one suggestion would be to compare the number of addresses in the array to the number in the output of the ip command for eth0 inside your up conditional. If that matches then everything should be up so exit which is your do nothing.
 
Old 08-24-2016, 07:32 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Why are you deleting the addresses from the interface if there is no problem?

Some suggestions can be found in the My Bash Blog link in my signature to help with debugging BASH scripts.

Things like using debug, setting variables and echoing out their values.
 
Old 08-24-2016, 09:07 AM   #4
horizn
Member
 
Registered: Jan 2015
Location: UK and Poland
Distribution: Slackware + Debian + Ubuntu
Posts: 170

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rtmistler View Post
Why are you deleting the addresses from the interface if there is no problem?

Some suggestions can be found in the My Bash Blog link in my signature to help with debugging BASH scripts.

Things like using debug, setting variables and echoing out their values.
To avoid situation where two servers/routers will have the same IP enabled. It is a VRRP cluster with two hosts.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
multiple if condition in a bash script grima Programming 4 03-28-2013 05:09 AM
[SOLVED] add options to bash script pablgonz Programming 15 08-20-2012 05:29 AM
add options to bash script pablgonz Programming 5 08-17-2012 03:29 PM
[SOLVED] Bash script to replace line break with condition spart1985 Linux - Newbie 8 02-23-2012 09:43 AM
add user bash script noir911 Programming 4 08-13-2005 08:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration