1
2
3
4
|
re. compile (strPattern[, flag]):
这个方法是Pattern类的工厂方法,用于将字符串形式的正则表达式编译为Pattern对象。 第二个参数flag是匹配模式,取值可以使用按位或运算符 '|' 表示同时生效,比如re.I | re.M。
另外,你也可以在regex字符串中指定模式,比如re. compile ( 'pattern' , re.I | re.M)与re. compile ( '(?im)pattern' )是等价的。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
xxx.com文件内容 $TTL 600 ; 1 day @ IN SOA ns1.xxxx.com. root.xxxx.com.( 1002766 ; Serial
3600 ; Refresh (1 hour)
900 ; Retry (15 minutes)
3600000 ; Expire (5 weeks 6 days 16 hours)
3600 ; Minimum (1 hour)
)
@ 2D IN NS ns1.xxxx.com. @ 2D IN NS ns2.xxxx.com. $ORIGIN xxxx.com. ;; IN MX 5 mxbiz1.qq.com.
IN MX 10 mxbiz2.qq.com.
IN TXT "v=spf1 include:spf.mail.qq.com ~all"
auth IN MX 5 mail.pub.jumei.com. auth IN TXT "v=spf1 ip4:x.x.x.x/24 ip4:x.x.x.x/24 ~all"
stk IN CNAME office.reemake.net. signontest IN CNAME xxx.xxxx.com. qqmaile33f7f2c IN CNAME mail.qq.com. *.kbs IN A 192.168.20.222 flow IN A 192.168.49.10 maven IN A 192.168.49.9 auth IN A 192.168.49.11 auth IN A 192.168.49.12 lynx-auth IN A 192.168.49.13 lynx-auth IN A 192.168.49.14 |
1
|
vim dns.py |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
#!/usr/bin/env python import sys
import re
import os
if __name__ = = '__main__' :
content = []
with open ( 'xxx.com' , 'r' ) as f:
with open ( 'xxx.com.bak' , 'w' ) as g:
#读取原文件行
for content in f.readlines():
#把执行脚本的第一个参数赋予变量x
x = sys.argv[ 1 ].strip()
#匹配拼接以x变量 + 多空格 IN 多空格 A 开头的行
#其中 r'' 是对引号中的字符串,保留字面,不进行转义
p = re. compile (r '^' + x + r '\s.+IN\s.+A' )
#p = re.compile(r'^'+x+r'\w*\s.+IN\s.+A')
#最后使用Match实例获得信息,如果不匹配re条件,写把行写到bak文件
if not p.match(content):
g.write(content)
#移动bak文件覆盖原来的老文件
os.rename( 'xxx.com.bak' , 'xxx.com' )
o = sys.argv[ 1 ].strip()
v = sys.argv[ 2 ].strip()
q = sys.argv[ 3 ].strip()
g.write( "%s\tIN\tA\t%s\n" % (o,v))
g.write( "%s\tIN\tA\t%s\n" % (o,q))
|
1
2
3
4
5
6
7
8
|
# ./dns.py auth 192.168.6.69 192.168.6.80 # diff xxx.com xxxx.com.bak 636,637d635 < auth IN A x.x.x.x < auth IN A x.x.x.x 801a800,801 > auth IN A 192.168.6.69 > auth IN A 192.168.6.80 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# cat test wms.web wms.rf wms.worker report.web report.worker admin invoice.web.center invoice.worker.center invoice.web. cd
invoice.worker. cd
jlsext jls jlsworker uds udsworker separate |
1
|
# for i in `cat test`;do ./dns.py $i 10.1.27.49 10.1.27.51;done
|
本文转自谢无赖51CTO博客,原文链接:http://blog.51cto.com/xieping/1899918 ,如需转载请自行联系原作者