for循环只运行一次(python)

时间:2015-06-05 13:56:29

标签: python for-loop

我在这里遇到这种逻辑很难过。我需要在函数process_dns_dump()中提供帮助。

似乎我只能运行一次代码,它不会遍历每一行。

我有一个.txt文档,我想阅读,而不是生成几个词典关联' SGSN'和GGSN'在同一个视图'。

代码如下:

            #!/usr/bin/env python

            def find_between( s, first, last ):
                try:
                    start = s.index( first ) + len( first )
                    end = s.index( last, start )
                    return s[start:end]
                except ValueError:
                    return "ERROR"
            def find_after( s, first):
                try:
                    start = s.index( first ) + len( first )
                    return s[start:]
                except ValueError:
                    return "ERROR"

            def process_dns_dump():
                SGSN = {'10.205.22.244' :'DRJO04 (E//)' , '189.40.240.0/26' : 'DRJO01' , '189.40.242.64/26' : 'DRJO02' , '10.204.13.216' : 'DRJO03 (E//)' ,
                            '189.40.112.0/27' : 'DRJO05_ATCA' , '189.40.115.0/27' : 'DRJO06_ATCA', '189.40.112.192/27' : 'DRJO07_ATCA', '189.40.115.128/26' : 'DRJO08_ATCA' ,
                            '189.40.115.64/26' : 'DRJO09_ATCA', '189.40.112.128/26' : 'DRJO10_ATCA', '189.40.166.0/27' : 'DRCE04_ATCA', '189.40.241.64/27' : 'DBHE01', '10.205.0.244' : 'DBHE02 (E//)',
                            '10.205.16.216' : 'DBLM01 (E//)', '189.40.241.192/26' : 'DBSA01', '10.205.18.216' : 'DBSA02 (E//)', '189.40.242.0/27' : 'DSPO01', '189.40.240.192/27' : 'DSPO02', '10.205.13.216' : 'DSPO03 (E//)',
                            '189.40.114.0/27' : 'DSPO06_ATCA', '189.40.114.192/26' : 'DSPO07_ATCA', '189.40.115.192/26' : 'DSPO08_ATCA', '189.40.114.32/27' : 'DSPO09_ATCA', '189.40.114.64/26' : 'DSPO10_ATCA', 
                            '189.40.114.128/26' : 'DSPO11_ATCA', '189.40.241.128/27' : 'DCTA01', '189.40.167.0/27' : 'DCTA03_ATCA', '10.203.7.244' : 'DSNE02 (E//)', '189.40.113.0/27' : 'DSNE04_ATCA', '10.205.9.216' : 'DRCE03',
                            '189.40.113.192/26' : 'DSNE05_ATCA', '189.40.167.160/27' : 'DMCTA1_Huawei', '189.40.166.160/27' : 'DMBSA1_Huawei', '189.40.166.128/27' : 'DMRCE1_Huawei', 
                            '189.40.166.192/27' : 'DMBHE1_Huawei', '189.40.166.224/27' : 'DMMNS1_Huawei', '189.40.167.128/27' : 'DMBLM1_Huawei', '189.40.167.192/27' : 'DMSDR1_Huawei'}

                GGSN = {'189.40.241.113' : 'GGSPO01', '189.40.240.233' : 'GGSNE01', '189.40.240.115' : 'FISNE01', '189.40.240.116' : 'FISNE02',
                            '189.40.98.65' : 'GPSPO2', '189.40.98.129' : 'GPRJO2', '189.40.241.249' : 'GGRJO02', '189.40.129.17' : 'GGRJO03',
                            '189.40.240.113' : 'FIRJO01', '189.40.112.112' : 'FNGRJO01', '189.40.166.96' : 'FNGRJO02', '189.40.113.112' : 'FNGSNE01',
                            '189.40.167.96' : 'FNGSNE02', '189.40.113.48' : 'FNGSNE03', '189.40.116.112' : 'FNGSNE05', '189.40.116.1' : 'FNGSPO01', '189.40.117.1' : 'FNGRJO03',
                            '189.40.116.129' : 'FNGSPO02', '189.40.116.48' : 'FNGSPO03', '189.40.117.129' : 'FNGRJO04', '189.40.117.33' : 'FNGRJO05', '189.40.117.80' : 'FNGRJO06',
                            '189.40.117.112' : 'FNGRJO07' }

                f = open(files)
                for lines in f:
                    if lines[0:3] == 'acl':
                        lista_SGSN = []
                        lista_GGSN = []
                        lista_IP_SGSN = find_between(lines, '{', ';};')
                        IP_SGSN = lista_IP_SGSN.split(';')
                        VIEW_DNS = str(find_between(lines, 'acl "SG.', '" '))



                        for value in IP_SGSN:
                            sgsn = str(SGSN.get(value))
                            if sgsn != 'None':
                                lista_SGSN.append(sgsn)

                        SGSN_final = find_between(str(lista_SGSN), '[', ']')
                        for linhas in f:
                            if linhas[0:3] == './d':
                                if str(find_between(linhas, 'mcc724.gprs.', ':timbrasil.br')) == VIEW_DNS:
                                    IP_GGSN = find_after(linhas, 'IN A ').replace('\n', '').rstrip()
                                    lista_GGSN.append(GGSN.get(IP_GGSN))
                                    lista_GGSN_unica = set(lista_GGSN)
                                    GGSN_final = str(find_between(str(lista_GGSN_unica),"set(['", "'])"))
                        sgsn_dump = {'VIEW' : VIEW_DNS,
                                     'SGSN' : SGSN_final,
                                     'GGSN' : GGSN_final}
                        print sgsn_dump




            files = 'teste.txt'

            process_dns_dump()

.txt文件是:

            acl "SG.ERS.BNF" {none;};
            acl "SG.ERS.SCO" {none;};
            acl "SG.ERS.SNE" {189.40.240.192/27;189.40.241.192/26;10.205.4.216;189.40.240.128/26;189.40.241.128/27;10.205.13.216;189.40.114.32/27;10.205.22.244;10.205.18.216;};
            acl "SG.ERS.VA" {189.40.167.0/27;189.40.113.0/27;189.40.114.0/27;189.40.114.192/26;189.40.242.0/27;189.40.113.192/26;189.40.115.192/26;189.40.114.64/26;189.40.114.128/26;189.40.167.160/27;189.40.166.160/27;};
            acl "SG.NSN.SCO" {189.40.112.0/27;189.40.166.0/27;189.40.240.0/26;189.40.240.64/27;189.40.112.192/27;189.40.241.64/27;10.205.0.244;10.205.16.216;189.40.241.0/27;189.40.115.128/26;189.40.242.64/26;189.40.115.64/26;189.40.112.128/26;189.40.166.128/27;189.40.166.192/27;189.40.166.224/27;189.40.167.128/27;189.40.167.192/27;189.40.115.0/27;};
            acl "SG.NSN.SNE" {none;};
            acl "SG.NSN.SNE_B" {none;};
            acl "SG.VAS.NORTE" {10.205.9.216;};
            acl "SG.VAS.SUL" {none;};
            ./db.mnc002.mcc724.gprs.ERS.BNF:timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.240.233 
            ./db.mnc002.mcc724.gprs.ERS.BNF:ipv6timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc002.mcc724.gprs.ERS.SCO:timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.117.1 
            ./db.mnc002.mcc724.gprs.ERS.SCO:timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.117.33 
            ./db.mnc002.mcc724.gprs.ERS.SCO:ipv6timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc002.mcc724.gprs.ERS.SNE:timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.240.233 
            ./db.mnc002.mcc724.gprs.ERS.SNE:timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.241.113 
            ./db.mnc002.mcc724.gprs.ERS.SNE:ipv6timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc002.mcc724.gprs.ERS.VA:timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.113.112 
            ./db.mnc002.mcc724.gprs.ERS.VA:timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.167.96 
            ./db.mnc002.mcc724.gprs.ERS.VA:timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.116.1 
            ./db.mnc002.mcc724.gprs.ERS.VA:timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.116.129 
            ./db.mnc002.mcc724.gprs.ERS.VA:ipv6timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc002.mcc724.gprs.LTE.NORTE:ipv6timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc002.mcc724.gprs.LTE.SUL:ipv6timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc002.mcc724.gprs.NSN.SCO:timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.117.129 
            ./db.mnc002.mcc724.gprs.NSN.SCO:timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.112.112 
            ./db.mnc002.mcc724.gprs.NSN.SCO:timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.166.96 
            ./db.mnc002.mcc724.gprs.NSN.SCO:timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.117.1 
            ./db.mnc002.mcc724.gprs.NSN.SCO:timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.117.33 
            ./db.mnc002.mcc724.gprs.NSN.SCO:ipv6timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc002.mcc724.gprs.NSN.SNE:timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.240.233 
            ./db.mnc002.mcc724.gprs.NSN.SNE:ipv6timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc002.mcc724.gprs.NSN.SNE_B:timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.240.233 
            ./db.mnc002.mcc724.gprs.NSN.SNE_B:ipv6timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc002.mcc724.gprs.VAS.NORTE:timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.116.112 
            ./db.mnc002.mcc724.gprs.VAS.NORTE:timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.116.48 
            ./db.mnc002.mcc724.gprs.VAS.NORTE:timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.117.112 
            ./db.mnc002.mcc724.gprs.VAS.NORTE:timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.117.80 
            ./db.mnc002.mcc724.gprs.VAS.NORTE:ipv6timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc002.mcc724.gprs.VAS.SUL:timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.240.233 
            ./db.mnc002.mcc724.gprs.VAS.SUL:ipv6timbrasil.br.mnc002.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc003.mcc724.gprs.ERS.BNF:timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.240.233 
            ./db.mnc003.mcc724.gprs.ERS.BNF:ipv6timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc003.mcc724.gprs.ERS.SCO:timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.117.33 
            ./db.mnc003.mcc724.gprs.ERS.SCO:timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.117.1 
            ./db.mnc003.mcc724.gprs.ERS.SCO:ipv6timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc003.mcc724.gprs.ERS.SNE:timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.240.233 
            ./db.mnc003.mcc724.gprs.ERS.SNE:timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.241.113 
            ./db.mnc003.mcc724.gprs.ERS.SNE:ipv6timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc003.mcc724.gprs.ERS.VA:timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.113.112 
            ./db.mnc003.mcc724.gprs.ERS.VA:timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.167.96 
            ./db.mnc003.mcc724.gprs.ERS.VA:timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.116.1 
            ./db.mnc003.mcc724.gprs.ERS.VA:timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.116.129 
            ./db.mnc003.mcc724.gprs.ERS.VA:ipv6timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc003.mcc724.gprs.LTE.NORTE:ipv6timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc003.mcc724.gprs.LTE.SUL:ipv6timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc003.mcc724.gprs.NSN.SCO:timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.117.129 
            ./db.mnc003.mcc724.gprs.NSN.SCO:timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.112.112 
            ./db.mnc003.mcc724.gprs.NSN.SCO:timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.166.96 
            ./db.mnc003.mcc724.gprs.NSN.SCO:timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.117.1 
            ./db.mnc003.mcc724.gprs.NSN.SCO:timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.117.33 
            ./db.mnc003.mcc724.gprs.NSN.SCO:ipv6timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc003.mcc724.gprs.NSN.SNE:timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.240.233 
            ./db.mnc003.mcc724.gprs.NSN.SNE:ipv6timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc003.mcc724.gprs.NSN.SNE_B:timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.240.233 
            ./db.mnc003.mcc724.gprs.NSN.SNE_B:ipv6timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc003.mcc724.gprs.VAS.NORTE:timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.116.48 
            ./db.mnc003.mcc724.gprs.VAS.NORTE:timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.117.80 
            ./db.mnc003.mcc724.gprs.VAS.NORTE:timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.117.112 
            ./db.mnc003.mcc724.gprs.VAS.NORTE:timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.116.112 
            ./db.mnc003.mcc724.gprs.VAS.NORTE:ipv6timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc003.mcc724.gprs.VAS.SUL:timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.240.233 
            ./db.mnc003.mcc724.gprs.VAS.SUL:ipv6timbrasil.br.mnc003.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc004.mcc724.gprs.ERS.BNF:timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.240.233 
            ./db.mnc004.mcc724.gprs.ERS.BNF:ipv6timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc004.mcc724.gprs.ERS.SCO:timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.117.33 
            ./db.mnc004.mcc724.gprs.ERS.SCO:timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.117.1 
            ./db.mnc004.mcc724.gprs.ERS.SCO:ipv6timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc004.mcc724.gprs.ERS.SNE:timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.240.233 
            ./db.mnc004.mcc724.gprs.ERS.SNE:timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.241.113 
            ./db.mnc004.mcc724.gprs.ERS.SNE:ipv6timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc004.mcc724.gprs.ERS.VA:timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.113.112 
            ./db.mnc004.mcc724.gprs.ERS.VA:timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.167.96 
            ./db.mnc004.mcc724.gprs.ERS.VA:timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.116.1 
            ./db.mnc004.mcc724.gprs.ERS.VA:timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.116.129 
            ./db.mnc004.mcc724.gprs.ERS.VA:ipv6timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc004.mcc724.gprs.LTE.NORTE:ipv6timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc004.mcc724.gprs.LTE.SUL:ipv6timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc004.mcc724.gprs.NSN.SCO:timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.117.129 
            ./db.mnc004.mcc724.gprs.NSN.SCO:timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.112.112 
            ./db.mnc004.mcc724.gprs.NSN.SCO:timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.166.96 
            ./db.mnc004.mcc724.gprs.NSN.SCO:timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.117.1 
            ./db.mnc004.mcc724.gprs.NSN.SCO:timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.117.33 
            ./db.mnc004.mcc724.gprs.NSN.SCO:ipv6timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc004.mcc724.gprs.NSN.SNE:timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.240.233 
            ./db.mnc004.mcc724.gprs.NSN.SNE:ipv6timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc004.mcc724.gprs.NSN.SNE_B:timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.240.233 
            ./db.mnc004.mcc724.gprs.NSN.SNE_B:ipv6timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc004.mcc724.gprs.VAS.NORTE:timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.116.48 
            ./db.mnc004.mcc724.gprs.VAS.NORTE:timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.117.80 
            ./db.mnc004.mcc724.gprs.VAS.NORTE:timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.117.112 
            ./db.mnc004.mcc724.gprs.VAS.NORTE:timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.116.112 
            ./db.mnc004.mcc724.gprs.VAS.NORTE:ipv6timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.113.112
            ./db.mnc004.mcc724.gprs.VAS.SUL:timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.240.233 
            ./db.mnc004.mcc724.gprs.VAS.SUL:ipv6timbrasil.br.mnc004.mcc724.gprs.        IN A 189.40.113.112

有人能给我一个亮点吗?我失去了希望。

谢谢!

2 个答案:

答案 0 :(得分:6)

你要两次迭代你的文件。来到这里:

for lines in f:

一旦到了这里,就在那个循环中。

for linhas in f:

但通常只能对文件进行一次迭代,然后“文件指针”指向文件的末尾,而您无法再提取任何数据。

一种可能的解决方案是将文件一次性读入字符串列表,然后迭代它。列表没有“只能迭代一次”的限制。

f = open(files)
line_collection = f.readlines()
for lines in line_collection:
    #...
    for linhas in line_collection:
        #etc

另一种解决方案是使用file.seek(0)将文件指针重置回文件的开头。但是在你的情况下执行似乎有点复杂,因为你可能想要在每次执行lines循环后寻找for linhas in line_collection当前持有的行的末尾,而不是寻求该文件的第一个字符。

答案 1 :(得分:0)

作为Kevin suggested,我使用字符串列表进行迭代,我需要多次迭代它。

#!/usr/bin/env python

def find_between( s, first, last ):
    try:
        start = s.index( first ) + len( first )
        end = s.index( last, start )
        return s[start:end]
    except ValueError:
        return "ERROR"
def find_after( s, first):
    try:
        start = s.index( first ) + len( first )
        return s[start:]
    except ValueError:
        return "ERROR"

def process_dns_dump():
    SGSN = {'10.205.22.244' :'DRJO04 (E//)' , '189.40.240.0/26' : 'DRJO01' , '189.40.242.64/26' : 'DRJO02' , '10.204.13.216' : 'DRJO03 (E//)' ,
                '189.40.112.0/27' : 'DRJO05_ATCA' , '189.40.115.0/27' : 'DRJO06_ATCA', '189.40.112.192/27' : 'DRJO07_ATCA', '189.40.115.128/26' : 'DRJO08_ATCA' ,
                '189.40.115.64/26' : 'DRJO09_ATCA', '189.40.112.128/26' : 'DRJO10_ATCA', '189.40.166.0/27' : 'DRCE04_ATCA', '189.40.241.64/27' : 'DBHE01', '10.205.0.244' : 'DBHE02 (E//)',
                '10.205.16.216' : 'DBLM01 (E//)', '189.40.241.192/26' : 'DBSA01', '10.205.18.216' : 'DBSA02 (E//)', '189.40.242.0/27' : 'DSPO01', '189.40.240.192/27' : 'DSPO02', '10.205.13.216' : 'DSPO03 (E//)',
                '189.40.114.0/27' : 'DSPO06_ATCA', '189.40.114.192/26' : 'DSPO07_ATCA', '189.40.115.192/26' : 'DSPO08_ATCA', '189.40.114.32/27' : 'DSPO09_ATCA', '189.40.114.64/26' : 'DSPO10_ATCA', 
                '189.40.114.128/26' : 'DSPO11_ATCA', '189.40.241.128/27' : 'DCTA01', '189.40.167.0/27' : 'DCTA03_ATCA', '10.203.7.244' : 'DSNE02 (E//)', '189.40.113.0/27' : 'DSNE04_ATCA', '10.205.9.216' : 'DRCE03',
                '189.40.113.192/26' : 'DSNE05_ATCA', '189.40.167.160/27' : 'DMCTA1_Huawei', '189.40.166.160/27' : 'DMBSA1_Huawei', '189.40.166.128/27' : 'DMRCE1_Huawei', 
                '189.40.166.192/27' : 'DMBHE1_Huawei', '189.40.166.224/27' : 'DMMNS1_Huawei', '189.40.167.128/27' : 'DMBLM1_Huawei', '189.40.167.192/27' : 'DMSDR1_Huawei'}

    GGSN = {'189.40.241.113' : 'GGSPO01', '189.40.240.233' : 'GGSNE01', '189.40.240.115' : 'FISNE01', '189.40.240.116' : 'FISNE02',
                '189.40.98.65' : 'GPSPO2', '189.40.98.129' : 'GPRJO2', '189.40.241.249' : 'GGRJO02', '189.40.129.17' : 'GGRJO03',
                '189.40.240.113' : 'FIRJO01', '189.40.112.112' : 'FNGRJO01', '189.40.166.96' : 'FNGRJO02', '189.40.113.112' : 'FNGSNE01',
                '189.40.167.96' : 'FNGSNE02', '189.40.113.48' : 'FNGSNE03', '189.40.116.112' : 'FNGSNE05', '189.40.116.1' : 'FNGSPO01', '189.40.117.1' : 'FNGRJO03',
                '189.40.116.129' : 'FNGSPO02', '189.40.116.48' : 'FNGSPO03', '189.40.117.129' : 'FNGRJO04', '189.40.117.33' : 'FNGRJO05', '189.40.117.80' : 'FNGRJO06',
                '189.40.117.112' : 'FNGRJO07' }

    f = open(files)
    line_collection = f.readlines()
    for lines in line_collection:
        if lines[0:3] == 'acl':
            lista_SGSN = []
            lista_GGSN = []
            lista_IP_SGSN = find_between(lines, '{', ';};')
            IP_SGSN = lista_IP_SGSN.split(';')
            VIEW_DNS = str(find_between(lines, 'acl "SG.', '" '))



            for value in IP_SGSN:
                sgsn = str(SGSN.get(value))
                if sgsn != 'None':
                    lista_SGSN.append(sgsn)

            SGSN_final = find_between(str(lista_SGSN), '[', ']')
            for linhas in line_collection:
                if linhas[0:3] == './d':
                    if str(find_between(linhas, 'mcc724.gprs.', ':timbrasil.br')) == VIEW_DNS:
                        IP_GGSN = find_after(linhas, 'IN A ').replace('\n', '').rstrip()
                        lista_GGSN.append(GGSN.get(IP_GGSN))
                        lista_GGSN_unica = set(lista_GGSN)
                        GGSN_final = str(find_between(str(lista_GGSN_unica),"set(['", "'])"))
            sgsn_dump = {'VIEW' : VIEW_DNS,
                         'SGSN' : SGSN_final,
                         'GGSN' : GGSN_final}
            print sgsn_dump

files = 'teste.txt'
process_dns_dump()