• python多目录字符串查找匹配


    1. 需求来自于实际工作: 需要处理一批服务器上运行的redis实例,每个redis实例可能有密码,也可能没有,有密码的,密码配置格式一定是: requirepass XXXXX # XXXX是密码

    2. 这些配置文件来自于各个某个目录下的各个子目录,因此,需要对这些目录进行递归查找

    3. 凡是该配置文件,一定以conf结尾作为文件名

    #coding:utf-8
    
    import re
    import os
    
    
    # pConf = re.compile(r'redis-|redis')
    pConf = re.compile(r'conf')
    pPassWord = re.compile(r'requirepass')
    
    upstreamFilesList = list()
    confFilesList = list()
    
    
    def getFileList(entry):
        for root, dirs, files in os.walk(entry):
            for file in files:
                if re.search(pConf, file.strip('\n')):
                    fileAbsPath = os.path.join(root, file)
                    confFilesList.append(fileAbsPath)
                else:
                    print 'not conf', file
        return confFilesList
    
    
    def getRedisAuth(filepath):
            findFlag = False
            with open(filepath + '' , 'r') as fd:
                while True:
                    line = fd.readline()
                    if line:
                        if re.findall(pPassWord, line.strip('\n')):
                            findFlag = True
                            print filepath, line.strip('\n').strip()
                    else:
                       break
                if not findFlag:
                   print filepath ,'nopassword'
    
    
    if __name__  == '__main__':
        with open('redis_single_ip.txt', 'r') as fd:
             while True:
                 ip = fd.readline().strip('\n')
                 if ip:
                    getFileList(ip)
                 else:
                    break
        for confFile in confFilesList:
             getRedisAuth(confFile)
    

      

      

  • 相关阅读:
    Python之语句与函数
    python语言的特别之处
    kafka消费者客户端
    kafka生产者客户端
    kafka技术分享02--------kafka入门
    kafka技术分享01--------why we study kafka?
    hadoop之hdfs及其工作原理
    hadoop之hdfs------------------FileSystem及其源码分析
    数据结构之红黑树(一)
    mysql中,唯一索引和普通索引应如何选择
  • 原文地址:https://www.cnblogs.com/haozike/p/python_multiDir_string_match.html
Copyright © 2020-2023  润新知