Junos 'EZ' automation library for remotely managing and automating Juniper Networks Junos devices through NETCONF protocol
—
Command execution capabilities for running CLI commands and RPC operations on Junos devices, including support for multiple output formats, structured and unstructured data retrieval, and XML processing.
Execute CLI commands on Junos devices with support for multiple output formats including text, XML, and JSON, along with timeout and normalization options.
def cli(self, command, format='text', warning=True):
"""
Execute CLI command on the device.
Parameters:
- command (str): CLI command to execute
- format (str): Output format ('text', 'xml', 'json')
- warning (bool): Display warning messages
Returns:
- str: Command output in requested format
Raises:
- RpcTimeoutError: Command execution timeout
- RpcError: Command execution error
"""Execute Remote Procedure Call (RPC) operations directly using XML structures, providing low-level access to all Junos device operations.
def execute(self, rpc_cmd, **kwargs):
"""
Execute RPC command on the device.
Parameters:
- rpc_cmd (Element): XML RPC command element
- **kwargs: Additional RPC parameters
- dev_timeout (int): RPC timeout in seconds
- filter_xml (str): XML filter for response
- ignore_warning (bool): Ignore warning messages
- normalize (bool): Normalize XML response
Returns:
- Element: XML response element
Raises:
- RpcTimeoutError: RPC execution timeout
- RpcError: RPC execution error
"""Access to dynamic RPC methods through the rpc property, allowing method-style access to all Junos RPC operations with automatic parameter handling.
@property
def rpc(self):
"""
RPC meta-object providing dynamic access to all Junos RPC operations.
Usage:
- dev.rpc.get_interface_information()
- dev.rpc.get_route_information(destination='0.0.0.0/0')
- dev.rpc.load_configuration(configuration=config_xml)
Returns:
- _RpcMetaExec: RPC meta-execution object
"""Utility functions for working with XML RPC commands, converting CLI commands to RPC format, and displaying RPC equivalents of CLI commands.
def display_xml_rpc(self, command, format='xml'):
"""
Display the XML RPC equivalent of a CLI command.
Parameters:
- command (str): CLI command
- format (str): Output format for the command
Returns:
- str: XML RPC representation of the CLI command
"""
def cli_to_rpc_string(self, command, format='text', dev_timeout=None):
"""
Convert CLI command to RPC string format.
Parameters:
- command (str): CLI command to convert
- format (str): Output format
- dev_timeout (int): Timeout for the operation
Returns:
- str: RPC string representation
"""Jinja2 template processing for configuration generation and complex command construction with variable substitution and logic.
def Template(self, filename):
"""
Load and return a Jinja2 template for configuration generation.
Parameters:
- filename (str): Template filename (searches current directory and module templates)
Returns:
- Template: Jinja2 template object
Usage:
- template = dev.Template('interface-config.j2')
- config = template.render(interface='ge-0/0/0', description='WAN Link')
"""from jnpr.junos import Device
dev = Device(host='router1.example.com', user='admin', passwd='secret')
dev.open()
# Execute CLI command with text output
version_info = dev.cli('show version')
print(version_info)
# Execute CLI command with XML output
interfaces_xml = dev.cli('show interfaces', format='xml')
# Execute CLI command with JSON output
interfaces_json = dev.cli('show interfaces', format='json')
dev.close()from jnpr.junos import Device
dev = Device(host='router1.example.com', user='admin', passwd='secret')
dev.open()
# Execute RPC using dynamic method access
interfaces = dev.rpc.get_interface_information()
# Execute RPC with parameters
routes = dev.rpc.get_route_information(destination='0.0.0.0/0')
# Execute RPC with detailed parameters
bgp_neighbors = dev.rpc.get_bgp_neighbor_information(
neighbor_address='192.168.1.1',
detail=True
)
dev.close()from jnpr.junos import Device
from jnpr.junos.exception import RpcTimeoutError
dev = Device(host='router1.example.com', user='admin', passwd='secret')
dev.open()
try:
# Long-running command with custom timeout
result = dev.cli('show route extensive', dev_timeout=120)
print("Command completed successfully")
except RpcTimeoutError:
print("Command timed out after 120 seconds")
dev.close()from jnpr.junos import Device
dev = Device(host='router1.example.com', user='admin', passwd='secret')
dev.open()
# See the XML RPC equivalent of a CLI command
rpc_xml = dev.display_xml_rpc('show interfaces ge-0/0/0')
print("RPC XML:")
print(rpc_xml)
# Convert CLI to RPC string
rpc_string = dev.cli_to_rpc_string('show route protocol bgp')
print("RPC String:")
print(rpc_string)
dev.close()from jnpr.junos import Device
dev = Device(host='router1.example.com', user='admin', passwd='secret')
dev.open()
# Load a Jinja2 template
template = dev.Template('interface-config.j2')
# Render template with variables
config = template.render(
interface='ge-0/0/0',
description='WAN Connection',
ip_address='192.168.1.1/24'
)
# Use rendered configuration
dev.cu.load(config, format='text')
dev.close()from jnpr.junos import Device
from lxml import etree
dev = Device(host='router1.example.com', user='admin', passwd='secret')
dev.open()
# Create custom RPC XML
rpc_xml = etree.Element('get-interface-information')
interface_elem = etree.SubElement(rpc_xml, 'interface-name')
interface_elem.text = 'ge-0/0/0'
# Execute custom RPC
result = dev.execute(rpc_xml)
# Process XML response
for interface in result.xpath('.//physical-interface'):
name = interface.findtext('name')
status = interface.findtext('oper-status')
print(f"Interface {name}: {status}")
dev.close()from jnpr.junos import Device
from jnpr.junos.exception import RpcError, RpcTimeoutError
dev = Device(host='router1.example.com', user='admin', passwd='secret')
dev.open()
try:
# Execute command that might fail
result = dev.cli('show interfaces ge-99/99/99')
except RpcError as e:
print(f"RPC Error: {e}")
print(f"Error message: {e.message}")
except RpcTimeoutError:
print("Command execution timed out")
dev.close()# CLI command and output types
CliCommand = str # CLI command string
CliOutput = str # CLI command output
# RPC types
RpcElement = object # XML Element for RPC commands/responses
RpcResponse = object # Parsed RPC response
# Template types
JinjaTemplate = object # Jinja2 template object
TemplateVars = dict[str, any] # Template variable dictionary
# Format options
OutputFormat = str # 'text', 'xml', 'json'Install with Tessl CLI
npx tessl i tessl/pypi-junos-eznc