Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tuple printing trouble
#1
Hi All,

trying to print interfaces details from router, below is the my code.

from ciscoconfparse import CiscoConfParse
interfaces = {}
cisco_cfg = CiscoConfParse("configuration.txt")
rtr_interfaces = cisco_cfg.find_objects(r"^interface")
for intfaces in rtr_interfaces:
    details = intfaces.children
    interfaces.update({'name': intfaces.text})
    for names in details:
        if 'vlan-type'in names.text:
           vlan = names.text.split(' ')[3]
           if not vlan:
                  interfaces.update({'vlan':' '})
           else:
                  interfaces.update({'vlan': vlan })
        if 'description' in names.text:
            description = names.text.split(' ')[2:]
            if not description:
               interfaces.update({'description': 'n/a'})
            else:
               interfaces.update({'description': description})
        if 'ip address' in names.text:
            ip = names.text.split(' ')[2:]
            if not ip:
               interfaces.update({'ip': 'n/a'})
            else:
               interfaces.update({'ip': ip})
        if 'qos-profile' in names.text:
            policy = names.text.split(' ')[2]
            if not policy:
               #interfaces.update({'policy': 'n/a'})
               if 'user-queue' in names.text:
                  user_queue = names.text.split(' ')[4]
                  if not user_queue:
                     interfaces.update({'policy': 'n/a'})
                  else:
                     interfaces.update({'policy':user_queue})
            else:   # if 'user-queue' in names.text:
               #policy = names.text.split(' ')[2]
               interfaces.update({'policy': policy})
    for details in interfaces.items():
        print("%s"%(details,))
Output:
('name', 'interface Eth-Trunk1.130') ('description', ['EXT_MISUMI', 'INDIA', 'PVT', 'LTD_12405738282']) ('vlan', '130') ('ip', ['address', '111.93.33.201', '255.255.255.248']) ('policy', '30Mb') ('name', 'interface Eth-Trunk1.155') ('description', ['EXT_IREO-12405068279']) ('vlan', '155') ('ip', ['address', '111.93.232.221', '255.255.255.252']) ('policy', 'Internet_8Mb') ('name', 'interface Eth-Trunk1.157') ('description', ['EXT_IREO-12405068279']) ('vlan', '155') ('ip', ['address', '111.93.232.221', '255.255.255.252']) ('policy', 'Internet_8Mb') ('name', 'interface Eth-Trunk1.158') ('description', ['EXT_MATHWORKS', 'INDIA', 'PRIVATE', 'LIMITED_1105725697']) ('vlan', '158') ('ip', ['address', '111.93.206.33', '255.255.255.252']) ('policy', '10Mb')
would like to print like below

name description vlan ip policy
interface Eth-Trunk1.158 EXT_MATHWORKS', 'INDIA', 'PRIVATE', 'LIMITED_1105725697 158 111.93.206.33', '255.255.255.252 10Mb
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,727 Nov-04-2020, 11:26 AM
Last Post: Aggam
  How to get first line of a tuple and the third item in its tuple. Need Help, Anybody? SukhmeetSingh 5 3,119 May-21-2019, 11:39 AM
Last Post: avorane
  I'm having trouble printing a return value in my main function RedSkeleton007 2 3,059 Apr-08-2018, 04:17 PM
Last Post: IAMK

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020