简体   繁体   English

Bash脚本:不断修改配置文件中的IP地址

[英]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. 我有一个bash数组$ {IP_ADDRESSES [@]},其中包含(您可能会猜到)IP地址。 This array gets updated every 10 minutes via cron. 此阵列通过cron每10分钟更新一次。

What I need to do, is 'dynamically' inject those IP addresses into an apache configuration file (/etc/httpd/conf.d/balancer.conf). 我需要做的是“动态地”将这些IP地址注入到apache配置文件(/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. 最初我有一个默认配置文件,但是我不想在其中添加任何“ BalancerMember”行。 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. 脚本识别出没有成员,然后查看$ {IP_ADDRESSES [@]},并看到其中包含3个IP地址。 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. 我有一个配置文件,它将在3个IP地址上工作。 Awesome. 太棒了 But what happens when the cron job next runs? 但是,下次执行cron作业时会发生什么? 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. a)没有任何变化,ip地址相同,没有配置文件被修改b)一个或多个IP地址已变化。 I would like to compare the ip addresses in the array with the ip addresses in the config file and replace it. 我想将阵列中的IP地址与配置文件中的IP地址进行比较并将其替换。 c) more or less IP addresses are in the array. c)阵列中包含或多或少的IP地址。 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 如果更多,请添加该数组中的所有IP地址

FOR EXAMPLE 例如

Everything is working well with the config files and the IP addresses in the array reflect what is in the configuration file. 配置文件一切正常,阵列中的IP地址反映了配置文件中的内容。 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. 但是,我们从数组中丢失了1.1.1.2。 It has been replaced by 1.1.1.4. 它已被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; 或者,假设1.1.1.4是一个附加IP地址,并且该阵列现在包含4个元素; 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: 或者,如果我们从阵列中丢失了2个IP地址(1.1.1.1和1.1.1.2),则配置文件应如下所示:

<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. 因此,最后,我只是尝试使配置文件与阵列中的IP地址保持最新。

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 1)通过检查md5sum echo ${array[*]} | md5sum来检查是否更新了echo ${array[*]} | md5sum echo ${array[*]} | md5sum you can save it in a temp file for subsequent comparisons. echo ${array[*]} | md5sum可以将其保存在临时文件中以进行后续比较。

2) Prepare a file with the syntax which changes every time array updates: 2)准备一个语法,该语法在每次数组更新时都会更改:

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 3)从现有文件中删除将使用tmpFile内容填充的现有行

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

4) Append the content(in right place) of file tmpFile to your exitingFile 4)将文件tmpFile的内容(在正确的位置)附加到您的exitingFile

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

Now your existingFile(which is your conf file) has updated IPs 现在您现有的文件(这是您的配置文件)已更新IP

Please note: 请注意:

yourPattern is pattern matching line just before lines with BalancerMember start in your original conf file. yourPattern是模式匹配行,恰好在原始conf文件中带有BalancerMember的行开始之前。

exitingFile is your existing conf file. exitingFile是您现有的conf文件。

and last sed statement will create a backupfile with .bck extension in you make some mistakes. 最后的sed语句会在您创建一些错误时创建一个扩展名为.bck的.bck

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. 您只需要该更新的数组值,如果您在同一脚本中执行操作,则只需检查数组是否已更新即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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