jnpr.junos

jnpr.junos.device

class jnpr.junos.device.Device(*vargs, **kvargs)[source]

Bases: object

Junos Device class.

ON_JUNOS:
READ-ONLY - Auto-set to True when this code is running on a Junos device, vs. running on a local-server remotely connecting to a device.
auto_probe:

When non-zero the call to open() will probe for NETCONF reachability before proceeding with the NETCONF session establishment. If you want to enable this behavior by default, you could do the following in your code:

from jnpr.junos import Device

# set all device open to auto-probe with timeout of 10 sec
Device.auto_probe = 10

dev = Device( ... )
dev.open()   # this will probe before attempting NETCONF connect
ON_JUNOS = False
Template(filename, parent=None, gvars=None)[source]

Used to return a Jinja2 Template.

Parameters:filename (str) – file-path to Jinja2 template file on local device
Returns:Jinja2 Template give filename.
__init__(*vargs, **kvargs)[source]

Device object constructor.

Parameters:
  • vargs[0] (str) – host-name or ipaddress. This is an alternative for host
  • host (str) – REQUIRED host-name or ipaddress of target device
  • user (str) – OPTIONAL login user-name, uses $USER if not provided
  • passwd (str) – OPTIONAL if not provided, assumed ssh-keys are enforced
  • port (int) – OPTIONAL NETCONF port (defaults to 830)
  • gather_facts (bool) – OPTIONAL default is True. If False then the facts are not gathered on call to open()
  • auto_probe (bool) – OPTIONAL if non-zero then this enables auto_probe at time of open() and defines the amount of time(sec) for the probe timeout
  • ssh_private_key_file (str) – OPTIONAL The path to the SSH private key file. This can be used if you need to provide a private key rather than loading the key into the ssh-key-ring/environment. if your ssh-key requires a password, then you must provide it via passwd
  • ssh_config (str) – OPTIONAL The path to the SSH configuration file. This can be used to load SSH information from a configuration file. By default ~/.ssh/config is queried.
  • normalize (bool) – OPTIONAL default is False. If True then the XML returned by execute() will have whitespace normalized
auto_probe = 0
bind(*vargs, **kvargs)[source]

Used to attach things to this Device instance and make them a property of the :class:Device instance. The most common use for bind is attaching Utility instances to a :class:Device. For example:

from jnpr.junos.utils.config import Config

dev.bind( cu=Config )
dev.cu.lock()
# ... load some changes
dev.cu.commit()
dev.cu.unlock()
Parameters:
  • vargs (list) –

    A list of functions that will get bound as instance methods to this Device instance.

    Warning

    Experimental.

  • new_property – name/class pairs that will create resource-managers bound as instance attributes to this Device instance. See code example above
cli(command, format='text', warning=True)[source]

Executes the CLI command and returns the CLI text output by default.

Parameters:
  • command (str) – The CLI command to execute, e.g. “show version”
  • format (str) – The return format, by default is text. You can optionally select “xml” to return the XML structure.

Note

You can also use this method to obtain the XML RPC command for a given CLI command by using the pipe filter | display xml rpc. When you do this, the return value is the XML RPC command. For example if you provide as the command show version | display xml rpc, you will get back the XML Element <get-software-information>.

Warning

This function is provided for DEBUG purposes only! DO NOT use this method for general automation purposes as that puts you in the realm of “screen-scraping the CLI”. The purpose of the PyEZ framework is to migrate away from that tooling pattern. Interaction with the device should be done via the RPC function.

Warning

You cannot use “pipe” filters with command such as | match or | count, etc. The only value use of the “pipe” is for the | display xml rpc as noted above.

close()[source]

Closes the connection to the device.

display_xml_rpc(command, format='xml')[source]

Executes the CLI command and returns the CLI text output by default.

Parameters:
  • command (str) – The CLI command to retrieve XML RPC for, e.g. “show version”
  • format (str) – The return format, by default is XML. You can optionally select “text” to return the XML structure as a string.
execute(*args, **kwargs)[source]

Executes an XML RPC and returns results as either XML or native python

Parameters:
  • rpc_cmd – can either be an XML Element or xml-as-string. In either case the command starts with the specific command element, i.e., not the <rpc> element itself
  • to_py' (func) –

    Is a caller provided function that takes the response and will convert the results to native python types. all kvargs will be passed to this function as well in the form:

    to_py( self, rpc_rsp, **kvargs )
    
Raises:
  • ValueError – When the rpc_cmd is of unknown origin
  • PermissionError – When the requested RPC command is not allowed due to user-auth class privilege controls on Junos
  • RpcError – When an rpc-error element is contained in the RPC-reply
Returns:

RPC-reply as XML object. If to_py is provided, then that function is called, and return of that function is provided back to the caller; presumably to convert the XML to native python data-types (e.g. dict).

facts
Returns:Device fact dictionary
facts_refresh(exception_on_failure=False)[source]

Reload the facts from the Junos device into facts property.

Parameters:exception_on_failure (bool) – To raise exception or warning when facts gathering errors out.
hostname
Returns:the host-name of the Junos device.
logfile
Returns:exsiting logfile file object.
manages
Returns:list of Resource Managers/Utilities attached to this instance using the bind() method.
open(*vargs, **kvargs)[source]

Opens a connection to the device using existing login/auth information.

Parameters:
  • gather_facts (bool) – If set to True/False will override the device instance value for only this open process
  • auto_probe (bool) – If non-zero then this enables auto_probe and defines the amount of time/seconds for the probe timeout
  • normalize (bool) – If set to True/False will override the device instance value for only this open process
Returns Device:

Device instance (self).

Raises:
  • ProbeError – When auto_probe is True and the probe activity exceeds the timeout
  • ConnectAuthError – When provided authentication credentials fail to login
  • ConnectRefusedError – When the device does not have NETCONF enabled
  • ConnectTimeoutError – When the the Device.timeout() value is exceeded during the attempt to connect to the remote device
  • ConnectError – When an error, other than the above, occurs. The originating Exception is assigned as err._orig and re-raised to the caller.
password
Returns:None - do not provide the password
probe(timeout=5, intvtimeout=1)[source]

Probe the device to determine if the Device can accept a remote connection. This method is meant to be called prior to :open(): This method will not work with ssh-jumphost environments.

Parameters:
  • timeout (int) – The probe will report True/False if the device report connectivity within this timeout (seconds)
  • intvtimeout (int) – Timeout interval on the socket connection. Generally you should not change this value, but you can if you want to twiddle the frequency of the socket attempts on the connection
Returns:

True if probe is successful, False otherwise

timeout
Returns:the current RPC timeout value (int) in seconds.
transform
Returns:the current RPC XML Transformation.
user
Returns:the login user (str) accessing the Junos device

jnpr.junos.exception

exception jnpr.junos.exception.CommitError(rsp, cmd=None, errs=None)[source]

Bases: jnpr.junos.exception.RpcError

Generated in response to a commit-check or a commit action.

__init__(rsp, cmd=None, errs=None)[source]
exception jnpr.junos.exception.ConfigLoadError(rsp, cmd=None, errs=None)[source]

Bases: jnpr.junos.exception.RpcError

Generated in response to a failure when loading a configuration.

__init__(rsp, cmd=None, errs=None)[source]
exception jnpr.junos.exception.ConnectAuthError(dev, msg=None)[source]

Bases: jnpr.junos.exception.ConnectError

Generated if the user-name, password is invalid

exception jnpr.junos.exception.ConnectClosedError(dev)[source]

Bases: jnpr.junos.exception.ConnectError

Generated if connection unexpectedly closed

__init__(dev)[source]
exception jnpr.junos.exception.ConnectError(dev, msg=None)[source]

Bases: exceptions.Exception

Parent class for all connection related exceptions

__init__(dev, msg=None)[source]
host

login host name/ipaddr

msg

login SSH port

port

login SSH port

user

login user-name

exception jnpr.junos.exception.ConnectNotMasterError(dev, msg=None)[source]

Bases: jnpr.junos.exception.ConnectError

Generated if the connection is made to a non-master routing-engine. This could be a backup RE on an MX device, or a virtual-chassis member (linecard), for example

exception jnpr.junos.exception.ConnectRefusedError(dev, msg=None)[source]

Bases: jnpr.junos.exception.ConnectError

Generated if the specified host denies the NETCONF; could be that the services is not enabled, or the host has too many connections already.

exception jnpr.junos.exception.ConnectTimeoutError(dev, msg=None)[source]

Bases: jnpr.junos.exception.ConnectError

Generated if the NETCONF session fails to connect, could be due to the fact the device is not ip reachable; bad ipaddr or just due to routing

exception jnpr.junos.exception.ConnectUnknownHostError(dev, msg=None)[source]

Bases: jnpr.junos.exception.ConnectError

Generated if the specific hostname does not DNS resolve

exception jnpr.junos.exception.LockError(rsp)[source]

Bases: jnpr.junos.exception.RpcError

Generated in response to attempting to take an exclusive lock on the configuration database.

__init__(rsp)[source]
exception jnpr.junos.exception.PermissionError(rsp, cmd=None, errs=None)[source]

Bases: jnpr.junos.exception.RpcError

Generated in response to invoking an RPC for which the auth user does not have user-class permissions.

PermissionError.message gives you the specific RPC that cause the exceptions

__init__(rsp, cmd=None, errs=None)[source]
exception jnpr.junos.exception.ProbeError(dev, msg=None)[source]

Bases: jnpr.junos.exception.ConnectError

Generated if auto_probe is enabled and the probe action fails

exception jnpr.junos.exception.RpcError(cmd=None, rsp=None, errs=None, dev=None, timeout=None, re=None)[source]

Bases: exceptions.Exception

Parent class for all junos-pyez RPC Exceptions

__init__(cmd=None, rsp=None, errs=None, dev=None, timeout=None, re=None)[source]
Cmd:is the rpc command
Rsp:is the rpc response (after <rpc-reply>)
Errs:is a list of dictionaries of extracted <rpc-error> elements.
Dev:is the device rpc was executed on
Timeout:is the timeout value of the device
Re:is the RE or member exception occured on
exception jnpr.junos.exception.RpcTimeoutError(dev, cmd, timeout)[source]

Bases: jnpr.junos.exception.RpcError

Generated in response to a RPC execution timeout.

__init__(dev, cmd, timeout)[source]
exception jnpr.junos.exception.SwRollbackError(rsp, re=None)[source]

Bases: jnpr.junos.exception.RpcError

Generated in response to a SW rollback error.

__init__(rsp, re=None)[source]
exception jnpr.junos.exception.UnlockError(rsp)[source]

Bases: jnpr.junos.exception.RpcError

Generated in response to attempting to unlock the configuration database.

__init__(rsp)[source]

jnpr.junos.jxml

jnpr.junos.jxml.INSERT(cmd)[source]
jnpr.junos.jxml.NAME(name)[source]
jnpr.junos.jxml.cscript_conf(reply)[source]
jnpr.junos.jxml.remove_namespaces(xml)[source]
jnpr.junos.jxml.rpc_error(rpc_xml)[source]

extract the various bits from an <rpc-error> element into a dictionary

jnpr.junos.rpcmeta

class jnpr.junos.rpcmeta._RpcMetaExec(junos)[source]

Bases: object

__init__(junos)[source]

~PRIVATE CLASS~ creates an RPC meta-executor object bound to the provided ez-netconf :junos: object

cli(command, format='text')[source]
get_config(filter_xml=None, options={})[source]

retrieve configuration from the Junos device

Filter_xml:is options, defines what to retrieve. if omitted then the entire configuration is returned
Options:is a dict, creates attributes for the RPC
load_config(contents, **options)[source]

loads :contents: onto the Junos device, does not commit the change.

Options:is a dictionary of XML attributes to set within the <load-configuration> RPC.

The :contents: are interpreted by the :options: as follows:

format=’text’ and action=’set’, then :contents: is a string containing a series of “set” commands

format=’text’, then :contents: is a string containing Junos configuration in curly-brace/text format

<otherwise> :contents: is XML structure