Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Formating a string to keep the wanted

I have an issue with this code:

from netmiko import ConnectHandler
from re import search

lsite=[]

try:
    NetworkDevice = {
        "host" : "ip",
        "username" : "username",
        "password" : "mdp",
        "device_type" : "cisco_ios"
    }

    connect = ConnectHandler(**NetworkDevice)
    connect.enable()
    command = "sh cdp nei"
    result = connect.send_command(command).strip()


    with open(r".\blabla.txt","w") as filout:
                filout.write(result)

    with open(r"blabla.txt") as temp_f:
        datafile = temp_f.readlines()
    for line in datafile:
        if search("AP", line):
            output = (f"{line}")
            lsite.append(output.strip())
            print(lsite[-1])
            

            
except(IndexError):
    pass

And I have this output:

056_AP_GRANDE_TERRE.AMCMZ.LAN
Gig 0/12          158             T B I  AIR-CAP16 Gig 0
054_AP_GRANDE_TERRE.AMCMZ.LAN
Gig 0/11          132             T B I  AIR-CAP16 Gig 0
052_AP_GRANDE_TERRE.AMCMZ.LAN
Gig 0/10          174             T B I  AIR-CAP16 Gig 0
050_AP_GRANDE_TERRE.AMCMZ.LAN
Gig 0/9           155             T B I  AIR-CAP16 Gig 0

I would like this output (maybe have to split in 2 outputs):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

056_AP_GRANDE_TERRE.AMCMZ.LAN
054_AP_GRANDE_TERRE.AMCMZ.LAN
052_AP_GRANDE_TERRE.AMCMZ.LAN
050_AP_GRANDE_TERRE.AMCMZ.LAN    

Gig 0/12    
Gig 0/11    
Gig 0/10    
Gig 0/9

I tried many ways with split() or strip() but I found nothing which works.

The print(lsite[-1]) is because only printing lsite shows me every lines who append my list like:

['056_AP_GRANDE_TERRE.AMCMZ.LAN']
['056_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/12          123             T B I  AIR-CAP16 Gig 0']
['056_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/12          123             T B I  AIR-CAP16 Gig 0', '054_AP_GRANDE_TERRE.AMCMZ.LAN']
['056_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/12          123             T B I  AIR-CAP16 Gig 0', '054_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/11          154             T B I  AIR-CAP16 Gig 0', 
'052_AP_GRANDE_TERRE.AMCMZ.LAN']
['056_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/12          123             T B I  AIR-CAP16 Gig 0', '054_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/11          154             T B I  AIR-CAP16 Gig 0', 
'052_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/10          142             T B I  AIR-CAP16 Gig 0']
['056_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/12          123             T B I  AIR-CAP16 Gig 0', '054_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/11          154             T B I  AIR-CAP16 Gig 0', 
'052_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/10          142             T B I  AIR-CAP16 Gig 0', '050_AP_GRANDE_TERRE.AMCMZ.LAN']
['056_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/12          123             T B I  AIR-CAP16 Gig 0', '054_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/11          154             T B I  AIR-CAP16 Gig 0', 
'052_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/10          142             T B I  AIR-CAP16 Gig 0', '050_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/9           177             T B I  AIR-CAP16 Gig 0']  
['056_AP_GRANDE_TERRE.AMCMZ.LAN']

>Solution :

I hoper this is what you need.

list_of_lists = [['056_AP_GRANDE_TERRE.AMCMZ.LAN'],
     ['056_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/12          123             T B I  AIR-CAP16 Gig 0'],
     ['056_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/12          123             T B I  AIR-CAP16 Gig 0',
      '054_AP_GRANDE_TERRE.AMCMZ.LAN'],
     ['056_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/12          123             T B I  AIR-CAP16 Gig 0',
      '054_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/11          154             T B I  AIR-CAP16 Gig 0',
      '052_AP_GRANDE_TERRE.AMCMZ.LAN'],
     ['056_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/12          123             T B I  AIR-CAP16 Gig 0',
      '054_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/11          154             T B I  AIR-CAP16 Gig 0',
      '052_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/10          142             T B I  AIR-CAP16 Gig 0'],
     ['056_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/12          123             T B I  AIR-CAP16 Gig 0',
      '054_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/11          154             T B I  AIR-CAP16 Gig 0',
      '052_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/10          142             T B I  AIR-CAP16 Gig 0',
      '050_AP_GRANDE_TERRE.AMCMZ.LAN'],
     ['056_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/12          123             T B I  AIR-CAP16 Gig 0',
      '054_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/11          154             T B I  AIR-CAP16 Gig 0',
      '052_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/10          142             T B I  AIR-CAP16 Gig 0',
      '050_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/9           177             T B I  AIR-CAP16 Gig 0'],
     ['056_AP_GRANDE_TERRE.AMCMZ.LAN']]

lans = set()
gigs = set()
for outer in list_of_lists:
    for inner in outer:
        if ".LAN" in inner:
            lans.add(inner)
        else:
            gigs.add(inner.split("  ")[0])

for lan in lans:
    print(lan)

for gig in gigs:
    print(gig)

Output:

050_AP_GRANDE_TERRE.AMCMZ.LAN
056_AP_GRANDE_TERRE.AMCMZ.LAN
052_AP_GRANDE_TERRE.AMCMZ.LAN
054_AP_GRANDE_TERRE.AMCMZ.LAN
Gig 0/11
Gig 0/12
Gig 0/9
Gig 0/10
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading