查找nginx 配置文件中有效的域名

#!/usr/bin/env python
##重慶話語科技---運維部--蓮花子
##python/shell
##羣號:35666534   歡迎你
import multiprocessing
import re,string,sys,urllib2,urllib,json,time,operator,optparse,os,fnmatch
import socket
menu=optparse.OptionParser()
menu.add_option("-l","--leixin",help="nginx config type")
menu.add_option("-s","--source",help="single source nginx config")
menu.add_option("-p","--path",help="source nginx config path")
opt,argv=menu.parse_args()
def check_s_r(file=None):
                BUF_SIZE = 1024
                domain=[]
                out=[]
                server_name="NO"
                bigfile = open(file,'r')
                tmp_lines = bigfile.readlines(BUF_SIZE)
                while tmp_lines:
                                for line in tmp_lines:
                                        if re.findall(r'(?<=server_name)\s+(.*);',line):
                                                server_name=re.findall(r'(?<=server_name)\s+(.*);',line)[0]
                                                for name in re.split(r'(\s+)',server_name):
                                                        if name.strip():
               domain.append(name.strip())
      return domain
                                tmp_lines = bigfile.readlines(BUF_SIZE)

def chkping(url):
    try: 
        ip=socket.gethostbyname(url)
        if not ip in ['0.0.0.0','127.0.0.1','10.0.0.1']:
            return(url,True)
        else:
            return (url,False)
    except:
            return (url,False)

def find(_path=None,_type=None):
        fuck={}
 pool = multiprocessing.Pool(processes = 7)
        for root, dirs, files in os.walk(_path):
                for f in files:
                        if fnmatch.fnmatch(f,_type):
    try:
     domain=check_s_r(os.path.join(root,f))
     for idomain in domain:
      if not fuck.has_key(f):
       fuck.update({idomain:f})
    except:
     pass
 s=pool.imap(chkping,fuck)
 for i in s:
  if i[1] == False:
   fuck.pop(i[0])
        pool.close()
        pool.join()
        sorted_x=sorted(fuck.iteritems(), key=lambda p:(p[0].split(".")[-2:5],p[0]),reverse=False ) 
        for i in sorted_x:
                        print "{0:-<30}\t{1:_>1}".format(i[0],i[1]);
def check_cmd():
        if opt.path != None and opt.leixin !=None:
                        find(opt.path,opt.leixin)
        elif opt.source != None :
                        check_s_r(opt.source)

check_cmd()
相關文章
相關標籤/搜索