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()
相关推荐
总的来说,"BAT批处理脚本-获取当前用户名和密码.zip"这个压缩包文件提供了一个获取当前活动用户名的示例,但获取密码的功能在Windows批处理脚本中并不直接可行。这提醒我们在使用自动化工具时,应充分理解其功能...
这一功能的实现主要涉及到本地安全策略(Local Security Policy)的修改,尤其是密码策略中的“密码必须满足复杂性要求”和“密码最长使用期限”这两项设置。 `noOverdue.bat`是这个脚本的核心,它使用批处理语言...
MySQL 8.0 是 MySQL 数据库管理系统的最新版本,提供了许多增强的功能和性能优化,旨在为开发者和企业级用户提供更高效、安全的数据存储和管理体验。这个“mysql8.0 附带一键安装脚本”可能包含了一个简化 MySQL 8.0...
3. **解除关联与清理**:脚本会找到与Microsoft Edge相关的注册表键和配置文件,并逐一删除,确保彻底卸载。这包括删除启动项、清除缓存、卸载更新等。 4. **重启相关服务**:为了确保所有更改生效,脚本可能还会...
"一键开启脚本"的作用在于,通过运行特定的脚本命令,自动完成黑域的启动和配置。这包括但不限于开启黑域服务、设置必要的权限、配置应用冻结规则等。使用这样的工具,非技术背景的用户也能轻松管理他们的手机性能,...
Centos7安装mongodb一键执行脚本,适用于以下版本: mongodb-linux-x86_64-rhel70-4.4.20.tgz; mongodb-linux-x86_64-rhel70-5.0.16.tgz; mongodb-linux-x86_64-rhel70-6.0.5.tgz; 将脚本和以上三个安装包拷贝至/...
ELK日志分析平台一键部署脚本 架构:logstash+elasticsearch+kibana 功能:shell脚本一键部署 亲测有用!!!
这个脚本会自动执行下载、解压、配置、编译和安装等一系列操作,大大减少了手动操作的繁琐。脚本通常包含以下功能: 1. **检查依赖**: 检查系统是否已经安装了所需的库,如果没有,自动下载并安装。 2. **设置变量...
按装autoIt,修改脚本对应的参数,IP、用户名、密码,自动完成输入。登录远程计算机。
你需要将所有必要的文件,包括Hadoop的配置文件(如core-site.xml、hdfs-site.xml、yarn-site.xml、mapred-site.xml等)、自定义的安装脚本,以及已经下载的JDK和Hadoop软件包,放置在同一个目录下。JDK和Hadoop的...
在远程连接时,用户需要知道服务器的IP地址或域名,并可能需要输入用户名和密码。对于安全起见,推荐使用SSH隧道加密连接,或者配置SSL证书以增强连接安全性。 总的来说,XRDP一键安装脚本简化了在Ubuntu系统上设置...
jdcookie.js下载 JD cookie一键获取脚本,基于JavaScript jdcookie.js下载 JD cookie一键获取脚本,基于JavaScript jdcookie.js下载 JD cookie一键获取脚本,基于JavaScript jdcookie.js下载 JD cookie一键获取脚本...
5. 安装Python:将编译后的Python二进制文件复制到系统路径,如/usr/local/bin。 6. 安装pip:通过Python的get-pip.py脚本获取并安装pip。 7. 安装virtualenv:利用pip安装virtualenv。 8. 验证安装:检查Python ...
在Linux世界中,为了提升效率,开发者们常常编写一键安装脚本来快速配置环境或安装软件。"linux-linux各类一键安装脚本懒人用懒办法"这个主题正体现了这一实践,它旨在帮助用户节省时间,避免繁琐的手动配置过程。...
ZABBIX一键安装脚本_zabbix-install-linux
oracle11g centos7一键安装脚本,脚本里面有完整安装提示说明。大概步骤如下 1、关闭防火墙 2、设置服务器名称并增加映射 3、安装Oracle所需的依赖 4、解压oracle安装包 5、创建用户和组 6、修改内核参数 7、修改用户...
Oracle DG安装一键安装脚本
【RDP Wrapper 配置文件更新脚本】 简介:由于 RDP Wrapper 多年未更新,自带的配置文件不支持新版本的远程桌面服务。因此我们只需更新配置文件即可。 用途:一键解决 RDP Wrapper 提示 Not listening [not ...
如果你质疑自己,不确定命令是否正确或者不确定脚本是否能正常执行, 你可以先测试,测试方法就是 在命令行输入 需要执行的命令,返回结果为理想状态就把命令放进脚本,并批量添加即可! 如:执行查询账号的其中一条...