需求如下:有nginx负载均衡配置文件,更新某几台服务器需要先注释掉前端机器,需要用脚本实现;
之前一直用sed进行替换,遇到一个高手用perl命令也可以实现,命令更简洁直观,可以实现同样的效果。
举例如下:
[root@localhost ~]# cat host.conf
upstream test_server {
server 192.168.169.36:80;
server 192.168.169.37:80;
server 192.168.169.38:80;
server 192.168.169.39:80;
server 192.168.169.50:80;
server 192.168.169.51:80;
server 192.168.169.52:80;
}
打印匹配的行
[root@localhost ~]# perl -ne 'print if /192.168.169.3/' host.conf
server 192.168.169.36:80;
server 192.168.169.37:80;
server 192.168.169.38:80;
server 192.168.169.39:80;
[root@localhost ~]# perl -ne 'print if /192.168.169.5/' host.conf
server 192.168.169.50:80;
server 192.168.169.51:80;
server 192.168.169.52:80;
perl命令:匹配192.168.169.3 网段的前面加#;
-i 写入到文件
[root@localhost ~]# perl -i -pe 's/(.*)/#$1/ if /192.168.169.3/' host.conf
执行结果如下:
[root@localhost ~]# cat host.conf
upstream test_server {
#server 192.168.169.36:80;
#server 192.168.169.37:80;
#server 192.168.169.38:80;
#server 192.168.169.39:80;
server 192.168.169.50:80;
server 192.168.169.51:80;
server 192.168.169.52:80;
}
同理,去掉#
[root@localhost ~]# perl -i -pe 's/#(.*)/$1/ if /192.168.169.3/' host.conf
[root@localhost ~]# cat host.conf
upstream test_server {
server 192.168.169.36:80;
server 192.168.169.37:80;
server 192.168.169.38:80;
server 192.168.169.39:80;
server 192.168.169.50:80;
server 192.168.169.51:80;
server 192.168.169.52:80;
}
匹配192.168.169. 网段的所有机器加#
[root@localhost ~]# perl -i -pe 's/(.*)/#$1/ if /192.168.169./' host.conf
[root@localhost ~]# cat host.conf
upstream test_server {
#server 192.168.169.36:80;
#server 192.168.169.37:80;
#server 192.168.169.38:80;
#server 192.168.169.39:80;
#server 192.168.169.50:80;
#server 192.168.169.51:80;
#server 192.168.169.52:80;
}
perl命令也支持类似sed的关键字替换
[root@localhost ~]# perl -pe 's/server/master/ if /192.168.169.3/' host.conf
upstream test_server {
master 192.168.169.36:80;
master 192.168.169.37:80;
master 192.168.169.38:80;
master 192.168.169.39:80;
server 192.168.169.50:80;
server 192.168.169.51:80;
server 192.168.169.52:80;
}
问题来了,实际配置文件基本都是同一个网段的ip,如果匹配的话,会匹配到其他机器上面,问题就严重了,所以需要按指定行进行精确匹配;
[root@localhost ~]# cat host.conf
upstream test_server {
server 192.168.169.36:80;
server 192.168.169.37:80;
server 192.168.169.38:80;
server 192.168.169.39:80;
server 192.168.169.50:80;
server 192.168.169.51:80;
server 192.168.169.52:80;
}
perl命令,匹配指定的行
if 判断 $. 行数,与或关系进行匹配;
[root@localhost ~]# perl -ne 'print if $.==2 ' host.conf
server 192.168.169.36:80;
[root@localhost ~]# perl -ne 'print if $.==4 ' host.conf
server 192.168.169.38:80;
[root@localhost ~]# perl -ne 'print if $.>=2 && $.<=5 ' host.conf
server 192.168.169.36:80;
server 192.168.169.37:80;
server 192.168.169.38:80;
server 192.168.169.39:80;
精确匹配,进行替换,大于等于第2行,小于等于第5行;
[root@localhost ~]# perl -pe 's/(.*)/#$1/ if $.>=2 && $.<=5' host.conf
upstream nav1_server {
#server 192.168.169.36:80;
#server 192.168.169.37:80;
#server 192.168.169.38:80;
#server 192.168.169.39:80;
server 192.168.169.50:80;
server 192.168.169.51:80;
server 192.168.169.52:80;
}
加-i 参数,将修改的操作写入到文件中;
[root@localhost ~]# perl -i -pe 's/(.*)/#$1/ if $.>=2 && $.<=5' host.conf