0

Here In my problem am trying to add data with the same key and value should be different but couldn't able to get the my desired output ..can anyone please help me in this.


     data1= """
       aaa-server AD-phoenix.ru protocol ldap
       max-failed-attempts 4
       realm-id 6
       aaa-server AD-phoenix protocol ldap
       aaa-server AD-phoenix.u protocol ldap 
       aaa-server AD-phoenix.u host 10.128.27.8
       aaa-server AD-phoenix.ru host 10.128.21.8
       aaa-server AD-phoenix host 10.128.21.8
       aaa-server AD-phoenix.ru host 10.128.21.9
       server-port 89
       """

     aaaserver_index_dict = {}
     aaaserver_dict ={}
     aaaserverIp_dict = {}
     for index,line in enumerate(data1.splitlines()):
       if 'aaa-server' in line and 'protocol ldap' in line:
        pattern = re.search(r'aaa-server (\S+) protocol ldap', line)
          if pattern and pattern.group(1) not in aaaserver_dict.keys():
            aaaserver_dict.setdefault(pattern.group(1), {}).update({"name":index})
            continue
       if 'aaa-server' in line and 'host' in line:
        pattern = re.search(r'aaa-server (\S+) host (\S+)', line)
         if pattern and pattern.group(1) not in aaaserver_dict.keys():
            aaaserver_dict.setdefault(pattern.group(1), {}).update({"name": index})
         if pattern and pattern.group(2) not in aaaserverIp_dict.keys():
            aaaserverIp_dict[pattern.group(2)] = pattern.group(1)
         else:
            aaaserverIp_dict[pattern.group(2)] = pattern.group(1) 
     print(aaaserver_dict) 
     print(aaaserverIp_dict)

desired output of **aaaserverIp_dict**:- {"10.128.27.8": 'AD-phoenix.u',"10.128.21.8":'AD-phoenix.ru',"10.128.21.8":'AD-phoenix',"10.128.21.9":'AD-phoenix.ru'}

gowthami
  • 239
  • 2
  • 13

0 Answers0