1) 数据库和相关应用的用户名, 密码等敏感信息加密之后集中存放在某一文件
2) Production Service 只需一键执行脚本即可把用户名和密码更新到指定路径下相应的配置文件(支持迭代).
#!/usr/bin/env python import os import sys import re # To read from credential file and put key/value pairs into a map def getCredentialMap(credentialFile): credentialsMap={} f = open(credentialFile, 'r') for line in f.readlines(): if not line.startswith('#'): tokens = line.split('=') credentialsMap['${' + tokens[0].strip() + '}'] = tokens[1].strip() return credentialsMap # To scan recursively from a specific top folder where there are configuration files need to update their credentials def scan(path, map, extensions, log): for i in os.walk(path): dirpath=i[0] dirnames=i[1] filenames=i[2] for j in filenames: processFile(os.path.join(dirpath, j), map, extensions, log) for k in dirnames: scan(os.path.join(dirpath, k), map, extensions, log) # To process a specific file def processFile(pathfile, map, extensions, log): extension = os.path.splitext(pathfile)[1] if extension not in extensions: return else: fh = open(pathfile, 'r+') newFile = '' updated = False lineNo = 0 for line in fh.readlines(): lineNo += 1 replaced, text = processLine(line, map) if replaced: updated = True log.write(pathfile + os.linesep) log.write('Line %3d: %s' % (lineNo, line.rstrip()) + os.linesep) log.write('========> %s' % (text.rstrip()) + os.linesep) newFile = newFile + text fh.close() if updated: fh = open(pathfile, 'r+') fh.truncate() fh.write(newFile) fh.close() # To process a specific line def processLine(line, map): text = line pattern = r'\${.+?}' replaced = False for match in re.finditer(pattern, line): s = match.start() e = match.end() key = line[s:e] value = map[key] if value: text = line.replace(key, value) replaced = True return (replaced, text) # The entry of this program def main(): sampleFile=''' #-----Follows are the content of a sample configuration file----- #path to the credentials file ssts.credentials.path=./credentials.properties #path(s) to the target top foder(s). Please separate with ";" when there is more than one target top folder ssts.target.path=./config;./build #the extentions of configuration files file.extention.filter=.xml;.properties #--------------------------------------------------------------- ''' configFile = os.path.splitext(sys.argv[0])[0] + '.properties' logFile = os.path.splitext(sys.argv[0])[0] + '.log' log = open(logFile, 'w') log.truncate() map = {} targetPath = [] try: fh = open(configFile, 'r') for line in fh.readlines(): if line.startswith('ssts.credentials.path'): credentialFile=line.split('=')[1].strip() if line.startswith('ssts.target.path'): targetPath=line.split('=')[1].strip().split(';') if line.startswith('file.extension.filter'): extensions=line.split('=')[1].strip().split(';') map = getCredentialMap(credentialFile) except IOError, e: print 'Please prepare ' + configFile + ' with this script ' + sys.argv[0], print sampleFile print e except Exception, e: print e for path in targetPath: scan(path, map, extensions, log) log.close() if __name__ == '__main__': main()
Groovy version:
#!/usr/bin/env groovy // To read from credential file and put key/value pairs into a map def getCredentialMap(String credentialFile){ credentialsMap=[:] def f = new File(credentialFile) f.eachLine{ if(!it.startsWith('#')){ def tokens = it.split('=') credentialsMap.put('${'+tokens[0].trim()+'}', tokens[1].trim()) } } return credentialsMap } // To scan recursively from a specific top folder where there are configuration files need to update their credentials def scan(path, map, extensions, log){ new File(path).eachFileRecurse { if(it.isFile()){ processFile(it, map, extensions, log) } } } // To process a specific file def processFile(file, map, extensions, log){ def extension = '.' + file.getName().split(/\./)[1] if(extension in extensions){ def newFile = new StringBuilder() def updated = false def lineNo = 0 file.eachLine{ lineNo += 1 def result = processLine(it, map) def replaced = result[0] def text = result[1] if(replaced){ updated = true log.append(file.getCanonicalFile().toString() + System.getProperty('line.separator')) log.append(String.format('Line %3d: %s', lineNo, it.trim()) + System.getProperty('line.separator')) log.append(String.format('========> %s', text.trim()) + System.getProperty('line.separator')) } newFile.append(text) } if(updated){ file.write('') file.write(newFile.toString()) } } } // To process a specific line def processLine(line, map){ def text = line def pattern = ~/\$\{.+?\}/ def replaced = false def matcher = pattern.matcher(line) def count = matcher.getCount() for(i in 0..<count){ def key = matcher[i] def value = map.get(key) if( value != null){ text = line.replace(key, value) replaced = true } } text = text + System.getProperty('line.separator') return [replaced, text] } def void MAIN(){ def sampleFile=''' #-----Follows are the content of a sample configuration file----- #path to the credentials file. (Don't input any space in between) suez.credentials.path=./ssts-credentials.properties #path(s) to the target top foder(s). Please separate with ";" when there is more than one target top folder. (Don't input any space in between) suez.target.path=./config;./build #the extentions of configuration files. (Don't input any space in between) file.extention.filter=.xml;.properties;.cfg #---------------------------------------------------------------- ''' def configFile = this.class.name + '.properties' def logFile = this.class.name + '.log' def log = new File(logFile) log.write('') def map = [:] def credentialFile def targetPath = [] def extensions = [] try{ new File(configFile).eachLine{ if(it.startsWith('suez.credentials.path')) credentialFile=it.split('=')[1].trim() if(it.startsWith('suez.target.path')) targetPath=it.split('=')[1].trim().split(';') if(it.startsWith('file.extension.filter')) extensions=it.split('=')[1].trim().split(';') } map = getCredentialMap(credentialFile) }catch(IOException e){ println 'Please prepare ' + configFile + ' for this script ' + this.class.name + '.groovy' println sampleFile println e }catch(Exception e){ println e } for(path in targetPath){ scan(path, map, extensions, log) } } MAIN()
相关推荐
该程序为wincc自定义界面的登陆脚本,希望能够帮助到大家。
hadoop-2.8.1一键搭建脚本和配置文件说明,快速搭建出分布式集群
mysql8.0 附带一键安装脚本
ELK日志分析平台一键部署脚本 架构:logstash+elasticsearch+kibana 功能:shell脚本一键部署 亲测有用!!!
gpmall商城 资源+一键部署脚本
Monkey脚本,一键执行
windows一键清理垃圾文件脚本
【RDP Wrapper 配置文件更新脚本】 简介:由于 RDP Wrapper 多年未更新,自带的配置文件不支持新版本的远程桌面服务。因此我们只需更新配置文件即可。 用途:一键解决 RDP Wrapper 提示 Not listening [not ...
Oracle DG安装一键安装脚本
如果你质疑自己,不确定命令是否正确或者不确定脚本是否能正常执行, 你可以先测试,测试方法就是 在命令行输入 需要执行的命令,返回结果为理想状态就把命令放进脚本,并批量添加即可! 如:执行查询账号的其中一条...
一键搭建FAS流程控脚本
jdcookie.js下载 JD cookie一键获取脚本,基于JavaScript jdcookie.js下载 JD cookie一键获取脚本,基于JavaScript jdcookie.js下载 JD cookie一键获取脚本,基于JavaScript jdcookie.js下载 JD cookie一键获取脚本...
nginx一键安装脚本
Odoo12一键安装脚本命令 下载文件成功后需要自行编辑一下 编辑包含删除文件内无用提示以及服务器安装时需要你指定的安装路径 编辑完成后以.sh为结尾保存文件即可在服务器通过命令一键执行安装
python语言结合ansible-playbook编写,大家...使用前提:执行脚本的机器上安装有ansible,配置好ansible到其他机器的ssh免密登录;执行脚本的机器上提前安装好python3;如遇到问题私信我,我看到消息一定会解答!!!
(2) 如果您没有该SQL Server服务器的Windows账户权限,需手动更改脚本中的Sql参数,配置相应的SQL用户名和密码即可。 压缩包里有: 1) BAT脚本 2) 3个SQL脚本实例(其中一个为脚本含有错误) 3) 说明文档(英文版)
shell脚本完成zabbix 3.4 版本的一键安装过程,减少新手或小白的操作难度,即装即用,快速体验。
openlookeng配置文件-一键启动脚本