Navigation
Guide That Contains This Content
[+] Expand All
[-] Collapse All

    Overview of Python with Junos Automation Enhancements

    Python is a programming language that lets you work more quickly and integrate your systems more effectively. The Python interpreter is included within the Junos operating system (Junos OS) jinstall-qfx-5-flex-x.tgz software bundle.

    Python is also suitable as an extension language for customizable applications.

    Starting with Junos OS Release 14.1X53-D10, the Open Source Python modules are pre-installed in the jinstall-qfx-5-flex-x.tgz software bundle. These modules are included:

    • ncclient—Facilitates client scripting and application development through the NETCONF protocol. See http://ncclient.grnet.gr/0.3.2/ for documentation of some of the external APIs of the ncclient Python module. At the bottom of this list, see examples of usage of some of these APIs with sample scripts.
    • lxml—Combines the speed and XML feature completeness of the C libraries libxml2 and libxslt with the simplicity of a native Python API. See http://lxml.de/tutorial.html/ for documentation of some of the external APIs of the lxml Python module.
    • jinja2—Serves as a fast, secure, designer-friendly templating language. See http://jinja.pocoo.org/docs/api/ for documentation of some of the external APIs of the jinja2 Python module.

    Example usage of some of the APIs of the ncclient Python module follows:

    * Example of "connect" and "command" API:

    from ncclient import manager
    
    def connect(host, port, user, password):
        conn = manager.connect(host=host,
                port=port,
                username=user,
                password=password,
                timeout=10,
                device_params = {'name':'junos'},
                hostkey_verify=False)
    
        print 'show version'
        print '*' * 30
        result = conn.command('show version', format='text')
        print result.xpath('output')[0].text
    
    if __name__ == '__main__':
        connect('router', '22', 'netconf', 'juniper!')

    * Example of "compare_configuration" API:

    from ncclient import manager
    from ncclient.xml_ import *
    
    import time
    
    def connect(host, port, user, password, source):
        conn = manager.connect(host=host,
                port=port,
                username=user,
                password=password,
                timeout=10,
                device_params = {'name':'junos'},
                hostkey_verify=False)
    
        compare_config = conn.compare_configuration(rollback=3)
        print compare_config.tostring
    
    if __name__ == '__main__':
        connect('router', 830, 'netconf', 'juniper!', 'candidate')

    * Example of "lock", "load_configuration", "validate", "commit", "discard_changes", "unlock" APIs:

    from ncclient import manager
    from ncclient.xml_ import *
    
    import time
    
    def connect(host, port, user, password, source):
        conn = manager.connect(host=host,
                port=port,
                username=user,
                password=password,
                timeout=10,
                device_params = {'name':'junos'},
                hostkey_verify=False)
    
        print 'locking configuration'
        lock = conn.lock()
    
        # build configuration element
        config = new_ele('system')
        sub_ele(config, 'host-name').text = 'foo'
        sub_ele(config, 'domain-name').text = 'bar'
    
        send_config = conn.load_configuration(config=config)
        print send_config.tostring
    
        check_config = conn.validate()
        print check_config.tostring
    
        compare_config = conn.compare_configuration()
        print compare_config.tostring
    
        print 'commit confirmed 300'
        #commit_config = conn.commit(confirmed=True, timeout='300')
        commit_config = conn.commit()
        print commit_config.tostring
    
        print 'sleeping for 5 sec...'
        time.sleep(5)
    
        discard_changes = conn.discard_changes()
        print discard_changes.tostring
    
        print 'unlocking configuration'
        unlock = conn.unlock()
        print unlock.tostring
    
    if __name__ == '__main__':
        connect('router', 830, 'netconf', 'juniper!', 'candidate')

    Note: For information on using Python, refer to your Python documentation.

    Release History Table

    Release
    Description

    Modified: 2016-12-22