简体   繁体   中英

Bash script: continually modifying IP addresses in configuration file

I think it is somewhat complicated what I am trying to do, but I'd love any help or suggestions:

I have a bash array ${IP_ADDRESSES[@]} that contains (as you might guess) IP addresses in it. This array gets updated every 10 minutes via cron.

What I need to do, is 'dynamically' inject those IP addresses into an apache configuration file (/etc/httpd/conf.d/balancer.conf). The syntax of the lines I am trying to modify are:

<Proxy balancer://cluster01)cluster>
BalancerMember ajp://<<ip-address>>:80 route=X

However this is where it gets tricky. I have the following requirements:

  • Initially I have a default configuration file, but I would not like to put any 'BalancerMember' lines in it. Instead, I would like to check the configuration file and see if it has any entries in it. -- If there are no entries found, go through the array and create those lines. The 'route' needs to be incremented by one

FOR EXAMPLE:

There is a configuration file with no balancer members. At this point, the part of the configuration file we are interested in looks like this:

<Proxy balancer://cluster01)cluster>

The Script recognises there are no members, and looks at ${IP_ADDRESSES[@]} and see it contains 3 IP addresses. For the first entry, underneath '' it inserts the first line. It now looks like this:

<Proxy balancer://cluster01)cluster>
BalancerMember ajp://1.1.1.1:80 route=0

Next, it finds adds the second and third:

<Proxy balancer://cluster01)cluster>
BalancerMember ajp://1.1.1.1:80 route=0
BalancerMember ajp://1.1.1.2:80 route=1
BalancerMember ajp://1.1.1.3:80 route=2

Everything is good at this point. I have a configuration file that will work over 3 ip addresses. Awesome. But what happens when the cron job next runs? There are a few things that could have happened:

a) nothing changes, the ip addresses are the same and no config files are modified b) one or more IP addresses have changed. I would like to compare the ip addresses in the array with the ip addresses in the config file and replace it. c) more or less IP addresses are in the array. If it's less than in the config file, delete any entries from the config file that are not in the array. If it's more, add any ip addresses in that are in the array

FOR EXAMPLE

Everything is working well with the config files and the IP addresses in the array reflect what is in the configuration file. It looks like this:

<Proxy balancer://cluster01)cluster>
BalancerMember ajp://1.1.1.1:80 route=0
BalancerMember ajp://1.1.1.2:80 route=1
BalancerMember ajp://1.1.1.3:80 route=2

However, we lose 1.1.1.2 from the array. It has been replaced by 1.1.1.4. The config file should now look like this:

<Proxy balancer://cluster01)cluster>
BalancerMember ajp://1.1.1.1:80 route=0
BalancerMember ajp://1.1.1.4:80 route=1
BalancerMember ajp://1.1.1.3:80 route=2

OR, say 1.1.1.4 is an additional IP address and the array now contains 4 elements; add it on to the end:

<Proxy balancer://cluster01)cluster>
BalancerMember ajp://1.1.1.1:80 route=0
BalancerMember ajp://1.1.1.2:80 route=1
BalancerMember ajp://1.1.1.3:80 route=2
BalancerMember ajp://1.1.1.4:80 route=3

etc.

OR, if we lose 2 IP addresses from the array (1.1.1.1 and 1.1.1.2), the config file should look like this:

<Proxy balancer://cluster01)cluster>
BalancerMember ajp://1.1.1.3:80 route=0

So in the end I'm just trying to keep the configuration file up to date with the IP addresses in an array.

Any and all help is GREATLY appreciated.

Cheers!!

In your script which updates content of array add

1) To check if array is updated of not by checking md5sum echo ${array[*]} | md5sum echo ${array[*]} | md5sum you can save it in a temp file for subsequent comparisons.

2) Prepare a file with the syntax which changes every time array updates:

for i in $(seq 0 $((${#klp[@]} - 1))) 
do 
echo "BalancerMember ajp ://${array[ $i ]}:80 route=$i" >> tmpFile
done

3) Delete existing lines which will be filled using tmpFile content from existingFile

 sed -i '/BalancerMember ajp:\/\/.*=[0-9]/d' <exitingFile>

4) Append the content(in right place) of file tmpFile to your exitingFile

sed -i'.bck' '/yourPattern/ r tmpFile' <exitingFile>

Now your existingFile(which is your conf file) has updated IPs

Please note:

yourPattern is pattern matching line just before lines with BalancerMember start in your original conf file.

exitingFile is your existing conf file.

and last sed statement will create a backupfile with .bck extension in you make some mistakes.

You can also implement this solution in a new script in case you do not want to change your existing one. You will just need that updated array value, its just your task of checking whether array is updated or not would be easy if you do it in same script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM