100 lines
3.1 KiB
Python
100 lines
3.1 KiB
Python
#!/usr/bin/env python
|
|
# -*- encoding: utf-8 -*-
|
|
"""
|
|
Topic: 根据一个替换map来替换ip地址
|
|
Desc :
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import re
|
|
import codecs
|
|
|
|
ip_maps = {
|
|
'10.0.0.0': '192.168.203.0',
|
|
'10.0.0.1': '192.168.203.254',
|
|
'10.0.0.255': '192.168.203.255',
|
|
# 下面是191机器
|
|
'10.0.0.191': '192.168.202.7',
|
|
'10.0.0.154': '192.168.203.92',
|
|
'10.0.0.159': '192.168.203.93',
|
|
'10.0.0.160': '192.168.203.94',
|
|
'10.0.0.171': '192.168.203.96',
|
|
'10.0.0.175': '192.168.203.95',
|
|
'10.0.0.183': '192.168.203.97',
|
|
'10.0.0.189': '192.168.203.98',
|
|
'10.0.0.203': '192.168.203.99',
|
|
'10.0.0.205': '192.168.203.100',
|
|
'10.0.0.207': '192.168.203.101',
|
|
'10.0.0.208': '192.168.203.102',
|
|
# 下面是201机器
|
|
'10.0.0.201': '192.168.202.3',
|
|
# 下面是181机器
|
|
'10.0.0.181': '192.168.202.5',
|
|
'10.0.0.182': '192.168.203.62',
|
|
'10.0.0.184': '192.168.203.63',
|
|
'10.0.0.185': '192.168.203.64',
|
|
'10.0.0.186': '192.168.203.65',
|
|
'10.0.0.187': '192.168.203.67',
|
|
'10.0.0.188': '192.168.203.66',
|
|
'10.0.0.190': '192.168.203.68',
|
|
'10.0.0.194': '192.168.203.69',
|
|
'10.0.0.202': '192.168.203.70',
|
|
'10.0.0.206': '192.168.203.71',
|
|
'10.0.0.209': '192.168.203.72',
|
|
# 下面是150机器
|
|
'10.0.0.150': '192.168.202.1',
|
|
'10.0.0.151': '192.168.203.2',
|
|
'10.0.0.152': '192.168.203.3',
|
|
'10.0.0.153': '192.168.203.5',
|
|
'10.0.0.157': '192.168.203.6',
|
|
'10.0.0.158': '192.168.203.7',
|
|
'10.0.0.162': '192.168.203.8',
|
|
'10.0.0.163': '192.168.203.9',
|
|
'10.0.0.164': '192.168.203.10',
|
|
'10.0.0.165': '192.168.203.11',
|
|
'10.0.0.166': '192.168.203.12',
|
|
'10.0.0.167': '192.168.203.13',
|
|
'10.0.0.168': '192.168.203.14',
|
|
'10.0.0.169': '192.168.203.15',
|
|
'10.0.0.170': '192.168.203.4',
|
|
'10.0.0.173': '192.168.203.16',
|
|
'10.0.0.174': '192.168.203.17',
|
|
'10.0.0.176': '192.168.203.18',
|
|
'10.0.0.177': '192.168.203.19',
|
|
'10.0.0.195': '192.168.203.20',
|
|
'10.0.0.196': '192.168.203.21',
|
|
'10.0.0.197': '192.168.203.22',
|
|
}
|
|
|
|
exclude_pat = (re.compile(r'.*\.bash_history'),
|
|
re.compile(r'.*\.log'),
|
|
re.compile(r'.*/ruby19/lib/.*'),
|
|
re.compile(r'.*/logs?/.*'),
|
|
re.compile(r'.*\.log\..*'),
|
|
re.compile(r'.*\.bak'),)
|
|
|
|
|
|
def search_replace(nfile):
|
|
"""paas环境的ip地址转换"""
|
|
with codecs.open(nfile, mode='r', encoding='utf-8') as nf:
|
|
file_names = nf.read().split('\n')
|
|
for fname in file_names:
|
|
if any(ep.match(fname) for ep in exclude_pat):
|
|
continue
|
|
try:
|
|
if os.path.isfile(fname):
|
|
with codecs.open(fname, mode='r', encoding='utf-8') as readf:
|
|
old_lines = readf.read()
|
|
for k, v in ip_maps.items():
|
|
old_lines = re.sub(k + r'(?=\D+|\n|$)', v, old_lines)
|
|
with codecs.open(fname, mode='w', encoding='utf-8') as writef:
|
|
writef.writelines(old_lines)
|
|
except UnicodeDecodeError:
|
|
pass
|
|
|
|
|
|
if __name__ == '__main__':
|
|
# sudo python replace_ip.py file_names
|
|
search_replace(sys.argv[1])
|