Junos EZNC (py-junos-eznc)
Junos EZNC is a Python library for automating Juniper Junos devices, simplifying network management tasks for non-programmers. It provides a high-level API to connect to devices, retrieve facts, execute commands, and manage configurations using NETCONF. The current version is 2.7.6, with releases occurring periodically to add features, fix bugs, and ensure compatibility with newer Python versions and underlying dependencies.
Common errors
-
ImportError: cannot import name 'Device' from 'jnpr.junos'
cause The Python environment does not have `junos-eznc` installed, or it's installed in a different environment, or the import path is incorrect.fixEnsure `junos-eznc` is installed via `pip install junos-eznc` in your active virtual environment. Verify the import statement: `from jnpr.junos import Device`. -
jnpr.junos.exception.ConnectError: Cannot connect to device: ('paramiko.ssh_exception.AuthenticationException', 'Authentication failed.')cause Incorrect username or password, or SSH key authentication failed. This can also occur if the user lacks necessary permissions on the Junos device.fixDouble-check credentials (username/password, SSH key path). Ensure the user is configured for NETCONF access on the Junos device. Verify network connectivity to the device's SSH port (22). -
ncclient.operations.rpc.RPCError: <rpc-error>
cause A NETCONF RPC call failed on the Junos device. This usually indicates a syntax error in the command sent, invalid configuration, or an operational issue on the device itself.fixExamine the full RPC error message for details. This often includes `error-message` and `error-path`. Consult Junos documentation for the correct syntax of the command or configuration being applied. -
AttributeError: 'Device' object has no attribute 'facts'
cause The `facts` attribute (or other operational attributes) is only available after a successful connection has been established using `dev.open()`.fixCall `dev.open()` before attempting to access `dev.facts` or other device attributes. Ensure connection success by handling `ConnectError`.
Warnings
- breaking Field names within some tables (e.g., `VlanTable`, `BfdSessionTable`) were changed in version 2.6.8. Existing scripts relying on old field names will break.
- gotcha PyEZ connections must be explicitly opened (`dev.open()`) and closed (`dev.close()`) to release resources. Forgetting to close can lead to lingering sessions or resource exhaustion on the device/client.
- gotcha Older versions of `junos-eznc` (prior to 2.7.6) may not fully support Python 3.12+ due to underlying dependency changes or internal updates.
- deprecated The `look_for_keys` parameter for SSH authentication was introduced to control SSH agent usage. Older versions might not have this explicit control, defaulting to agent usage.
Install
-
pip install junos-eznc
Imports
- Device
import junos_eznc
from jnpr.junos import Device
- ConnectError
from jnpr.junos.exception import ConnectError
Quickstart
import os
from jnpr.junos import Device
from jnpr.junos.exception import ConnectError
host = os.environ.get('JUNOS_HOST', 'your_junos_device_ip')
user = os.environ.get('JUNOS_USER', 'your_username')
password = os.environ.get('JUNOS_PASSWORD', 'your_password')
dev = None
try:
# hostkey_verify=False for labs/quick starts, use True in production
dev = Device(host=host, user=user, password=password, port=22, hostkey_verify=False)
dev.open()
print(f"Connected to device: {dev.hostname}")
print(f"Device facts: {dev.facts['hostname']}, {dev.facts['version']}")
# Example: Execute a command
# rpc_resp = dev.rpc.get_system_uptime_information()
# print(rpc_resp.find('.//up-time').text)
except ConnectError as err:
print(f"Connection error: {err}")
except Exception as err:
print(f"An error occurred: {err}")
finally:
if dev and dev.connected:
dev.close()
print("Disconnected from device.")