Automation

Expand all | Collapse all

[PyEz] Properly format get_config

  • 1.  [PyEz] Properly format get_config

    Posted 07-03-2017 14:15

    Hello,

     

    I am in the process of writing a Python script that uses PyEz to run some troubleshooting commands for our support team. The goal is to allow a teir 1 support person to do the basic troubleshooting with the need to have direct access to a switch or router.

     

    In this current example, I am trying to return the configuration of an interface. I having issues getting the text version to print in a pretty format (all the proper return and space/tab characters. More information below.

     

    XML format data of interface:

     

    config = dev.rpc.get_config(filter_xml=etree.XML('<configuration><interfaces><interface><name>%s</name></interface></interfaces></configuration>' % args.interface), options={'format':'xml', 'database':'committed', 'inherit':'inherit'})

    print(etree.tostring(config, pretty_print=True))

     

    See the attachment pyez_XML_output.jpg for the sample output. 

     

    Text format data of interface:

     

    config = dev.rpc.get_config(filter_xml=etree.XML('<configuration><interfaces><interface><name>%s</name></interface></interfaces></configuration>' % args.interface), options={'format':'text', 'database':'committed', 'inherit':'inherit'})
    print(etree.tostring(config, pretty_print=True))

     

    See the attachment pyez_TEXT_output.jpg for the sample output

     

    The text version seems to print everything in a single line, instead of the output I see when running via cli. 

     

    I am looking to see if there is an easy way to print the text version with proper return, and tab characters. Example of what I wanted to see in attachment pyez_TEXT_desired_output.jpg

     

    I have tired to see if there are any examples out there for this model, but I have not be able to locate anything yet. 

     

    Thanks for the help


    #pyez
    #get_config


  • 2.  RE: [PyEz] Properly format get_config

    Posted 07-11-2017 02:38

    Can you please share junos details. I am not facing this issue with any of the device in my lab. For faster communication wrt to PyEZ/netconf please write to jnpr-community-netdev@juniper.net



  • 3.  RE: [PyEz] Properly format get_config

    Posted 07-11-2017 10:15

    I suspect you may be specifying normalize=True in your call to Device() or open(). Is my suspicion correct?

     

    While normalize=True is often useful, you need to be aware that it also normalizes interior whitespace/newlines of text values, not just the whitespace/newlines at the beginning and end of the text value.

     

    If this is indeed the case you are seeing, then you can add normalize=False to dev.rpc.get_config().

     

    Here's an example:

    #!/usr/bin/env python
    
    import getpass
    import sys
    from jnpr.junos import Device
    from lxml import etree
    
    # Get username and password as user input.
    # Assumes all devices have same user/pass
    user = raw_input('Device Username: ')
    password = getpass.getpass('Device Password for: ')
    
    #Loop through each host specified on the command line.
    for hostname in sys.argv[1:]:
        dev = Device(host=hostname, user=user, passwd=password, normalize=True)
        print("Opening %s." % (hostname))
        dev.open()
        resp = dev.rpc.get_config(filter_xml=etree.XML("""<configuration>
                                                              <interfaces>
                                                                  <interface>
                                                                      <name>lo0</name>
                                                                  </interface>
                                                              </interfaces>
                                                          </configuration>"""),
                                  normalize=False,
                                  options={'inherit':'inherit',
                                           'database':'committed',
                                           'format':'text'})
        if resp is not None:
            print("Configuration:\n%s" % (resp.text))
    

    Here's the result of running this example:

    Device Username: user
    Device Password for: 
    Opening r0.
    Configuration:
    
    ## Last commit: 2017-07-10 11:14:58 PDT by user
    interfaces {
        lo0 {
            unit 0 {
                family inet {
                    address 127.0.0.1/32;
                    address 10.255.12.33/32 {
                        primary;
                    }
                }
                family iso {
                    address 47.0005.80ff.f800.0000.0108.0001.0102.5501.2033.00;
                }
                family inet6 {
                    address abcd::10:255:12:33/128 {
                        primary;
                    }
                }
            }
        }
    }
    

    Please be aware of #757 which I encountered while creating/testing the above example script. You can avoid this issue by making sure you specify normalize=True in the Device() call (as I've done in my example) rather than in the open() call. I've got a proposed fix for the issue, but it might take a couple of days to get the fix review and merged.