天天看點

python fileinput 子產品替換檔案全局内容

nginx.conf 檔案有如下内容:

 server  {    .....................................................................   

                       server 192.168.201.131:80 weight=1;

server 192.168.201.132:80 weight=1;

server 192.168.201.133:80 weight=1;

                        server 192.168.201.136:80 weight=1;

server 192.168.201.137:80 weight=1;

server 192.168.201.139:80 weight=1;

server 192.168.205.2:80 weight=1;

          server 192.168.205.3:80 weight=1;

         server 192.168.205.4:80 weight=1;

         server 192.168.205.5:80 weight=1;

         server 192.168.205.6:80 weight=1;

         server 192.168.205.7:80 weight=1;

         server 192.168.205.8:80 weight=1;  

         server 192.168.205.9:80 weight=1;  

                        server 192.168.205.10:80 weight=1;  

                        server 192.168.205.11:80 weight=1;  

                        server 192.168.205.12:80 weight=1;  

                        server 192.168.205.13:80 weight=1;  

                        server 192.168.205.14:80 weight=1;  

                        server 192.168.205.15:80 weight=1;  

                        server 192.168.205.16:80 weight=1;  

                      server 192.168.205.17:80 weight=1;  

                      server 192.168.205.18:80 weight=1;  

                      server 192.168.205.19:80 weight=1;  

                      server 192.168.205.20:80 weight=1;  

                      server 192.168.205.21:80 weight=1;

                      server 192.168.205.22:80 weight=1;

                      server 192.168.205.23:80 weight=1;

                      server 192.168.205.24:80 weight=1;

                      server 192.168.205.25:80 weight=1;

                      server 192.168.205.26:80 weight=1;

                      server 192.168.205.27:80 weight=1;

                      server 192.168.205.28:80 weight=1;

                      server 192.168.205.29:80 weight=1;

                      server 192.168.205.30:80 weight=1;

                      server 192.168.205.31:80 weight=1;

                      server 192.168.205.32:80 weight=1;

                      server 192.168.205.33:80 weight=1;

                      server 192.168.205.34:80 weight=1;

                      server 192.168.205.35:80 weight=1;

                      server 192.168.205.36:80 weight=1;

                      server 192.168.205.37:80 weight=1;

                      server 192.168.205.38:80 weight=1;

                      server 192.168.205.39:80 weight=1;

                      server 192.168.205.40:80 weight=1;

                      server 192.168.205.41:80 weight=1;

}

在上面所有205網段上面加上#登出号,python 實作腳本如下:

#!/bin/python

#coding=utf-8

import fileinput

for line in fileinput.input('nginx.conf.erb201',inplace=1):

     line = line.replace('server 192.168.205.','#server 192.168.205.')

     print line,

     本文轉自yzy121403725 51CTO部落格,原文連結:http://blog.51cto.com/lookingdream/1923319,如需轉載請自行聯系原作者