jnpr.junos¶
jnpr.junos.device¶
- class jnpr.junos.device.Device(*args, **kwargs)[source]¶
Bases:
_ConnectionJunos Device class.
ON_JUNOS:READ-ONLY - Auto-set to
Truewhen 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
- __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, unless sock_fd is provided
sock_fd (str) – REQUIRED file descriptor of an existing socket instead of providing a host. Used for outbound ssh.
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 For ssh mode default is
True. In case of console connection over telnet/serial it defaults toFalse. IfFalseand old-style fact gathering is in use then facts are not gathered on call toopen(). This argument is a no-op when new-style fact gathering is in use (the default.)fact_style (str) – OPTIONAL The style of fact gathering to use. Valid values are: ‘new’, ‘old’, or ‘both’. The default is ‘new’. The value ‘both’ is only present for debugging purposes. It will be removed in a future release. The value ‘old’ is only present to workaround bugs in new-style fact gathering. It will be removed in a future release.
mode (str) – OPTIONAL mode, mode for console connection (telnet/serial)
baud (int) – OPTIONAL baud, Used during serial console mode, default baud rate is 9600
attempts (int) – OPTIONAL attempts, for console connection. default is 10
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 timeoutssh_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. IfTruethen the XML returned byexecute()will have whitespace normalizeduse_filter (bool) – OPTIONAL To choose between SAX and DOM parsing. default is
Falseto use DOM. SelectTrueto use SAX (if SAX input is provided).conn_open_timeout (int) – OPTIONAL To specify the timeout in seconds, which will be used while opening SSH connection to the device
huge_tree (bool) – OPTIONAL parse XML with very deep trees and long text content. default is
False.look_for_keys (bool) – OPTIONAL To disable public key authentication. default is
None.allow_agent (bool) – OPTIONAL Specifies whether to use keys provided by an SSH agent for authentication. If set to
True, the SSH connection will use any keys loaded in the agent. If set toFalse, keys from the SSH agent will not be used. If set toNone, the default behavior is applied: agent keys are used only if both password and private key file are not provided. Default isNone.bind_addr (str) – OPTIONAL To use (local) source IP address. default is
None.hostkey_verify (bool) – OPTIONAL To enable ssh_known hostkey verify default is
False.proxy_command (str) – OPTIONAL The SSH ProxyCommand string to use when connecting through a bastion/jump host, e.g.
"ssh -W %h:%p bastion.example.com". Wrapsparamiko.proxy.ProxyCommandand is passed as thesockargument to the underlying ncclient transport. Cannot be combined withsock_fd.
- property connected¶
- open(*vargs, **kvargs)[source]¶
Opens a connection to the device using existing login/auth information.
- Parameters:
gather_facts (bool) – If set to
True/Falsewill override the device instance value for only this open processauto_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/Falsewill override the device instance value for only this open process
- Returns Device:
Device instance (self).
- Raises:
ProbeError – When auto_probe is
Trueand the probe activity exceeds the timeoutConnectAuthError – 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 deviceConnectError – When an error, other than the above, occurs. The originating
Exceptionis assigned aserr._origand re-raised to the caller.
- property transform¶
- Returns:
the current RPC XML Transformation.
jnpr.junos.exception¶
- exception jnpr.junos.exception.CommitError(rsp, cmd=None, errs=None)[source]¶
Bases:
RpcErrorGenerated in response to a commit-check or a commit action.
- __init__(rsp, cmd=None, errs=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.ConfigLoadError(rsp, cmd=None, errs=None)[source]¶
Bases:
RpcErrorGenerated in response to a failure when loading a configuration.
- __init__(rsp, cmd=None, errs=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.ConnectAuthError(dev, msg=None)[source]¶
Bases:
ConnectErrorGenerated if the user-name, password is invalid
- exception jnpr.junos.exception.ConnectClosedError(dev)[source]¶
Bases:
ConnectErrorGenerated if connection unexpectedly closed
- exception jnpr.junos.exception.ConnectError(dev, msg=None)[source]¶
Bases:
ExceptionParent class for all connection related exceptions
- property host¶
login host name/ipaddr
- property msg¶
login SSH port
- property port¶
login SSH port
- property user¶
login user-name
- exception jnpr.junos.exception.ConnectNotMasterError(dev, msg=None)[source]¶
Bases:
ConnectErrorGenerated 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:
ConnectErrorGenerated 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:
ConnectErrorGenerated 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:
ConnectErrorGenerated if the specific hostname does not DNS resolve
- exception jnpr.junos.exception.FactLoopError[source]¶
Bases:
RuntimeErrorGenerated when there is a loop in fact gathering.
- exception jnpr.junos.exception.JSONLoadError(exception, rpc_content)[source]¶
Bases:
ExceptionGenerated if json content of rpc reply fails to load
- exception jnpr.junos.exception.LockError(rsp)[source]¶
Bases:
RpcErrorGenerated in response to attempting to take an exclusive lock on the configuration database.
- exception jnpr.junos.exception.PermissionError(rsp, cmd=None, errs=None)[source]¶
Bases:
RpcErrorGenerated 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]¶
- 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.ProbeError(dev, msg=None)[source]¶
Bases:
ConnectErrorGenerated 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:
ExceptionParent 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:
RpcErrorGenerated in response to a RPC execution timeout.
- __init__(dev, cmd, timeout)[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.SwRollbackError(rsp, re=None)[source]¶
Bases:
RpcErrorGenerated in response to a SW rollback error.
jnpr.junos.jxml¶
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
- get(filter_select=None, ignore_warning=False, **kwargs)[source]¶
Retrieve running configuration and device state information using <get> rpc
dev.rpc.get() dev.rpc.get(ignore_warning=True) dev.rpc.get(filter_select='bgp') or dev.rpc.get('bgp') dev.rpc.get(filter_select='bgp/neighbors') dev.rpc.get("/bgp/neighbors/neighbor[neighbor-address='10.10.0.1']" "/timers/state/hold-time") dev.rpc.get('mpls', ignore_warning=True)
- Parameters:
filter_select (str) – The select attribute will be treated as an XPath expression and used to filter the returned data.
ignore_warning –
A boolean, string or list of string. If the value is True, it will ignore all warnings regardless of the warning message. If the value is a string, it will ignore warning(s) if the message of each warning matches the string. If the value is a list of strings, ignore warning(s) if the message of each warning matches at least one of the strings in the list.
For example:
dev.rpc.get(ignore_warning=True) dev.rpc.get(ignore_warning='vrrp subsystem not running') dev.rpc.get(ignore_warning=['vrrp subsystem not running', 'statement not found'])
Note
When the value of ignore_warning is a string, or list of strings, the string is actually used as a case-insensitive regular expression pattern. If the string contains only alpha-numeric characters, as shown in the above examples, this results in a case-insensitive substring match. However, any regular expression pattern supported by the re library may be used for more complicated match conditions.
- Returns:
xml object
- get_config(filter_xml=None, options={}, model=None, namespace=None, remove_ns=True, **kwargs)[source]¶
retrieve configuration from the Junos device
dev.rpc.get_config() dev.rpc.get_config(filter_xml='<system><services/></system>') dev.rpc.get_config(filter_xml='system/services') dev.rpc.get_config( filter_xml=etree.XML('<system><services/></system>'), options={'format': 'json'}) # to fetch junos as well as yang model configs dev.rpc.get_config(model=True) # openconfig yang example dev.rpc.get_config(filter_xml='bgp', model='openconfig') dev.rpc.get_config(filter_xml='<bgp><neighbors></neighbors></bgp>', model='openconfig') # custom yang example dev.rpc.get_config(filter_xml='l2vpn', model='custom', namespace="http://yang.juniper.net/customyang/l2vpn") # ietf yang example dev.rpc.get_config(filter_xml='interfaces', model='ietf') # ietf-softwire yang example dev.rpc.get_config(filter_xml='softwire-config', model='ietf', namespace="urn:ietf:params:xml:ns:yang:ietf-softwire", options={'format': 'json'})
- Filter_xml:
fully XML formatted tag which defines what to retrieve, when omitted the entire configuration is returned; the following returns the device host-name configured with “set system host-name”
config = dev.rpc.get_config(filter_xml=etree.XML(''' <configuration> <system> <host-name/> </system> </configuration>'''))
- Options:
is a dictionary of XML attributes to set within the <get-configuration> RPC; the following returns the device host-name either configured with “set system host-name” and if unconfigured, the value inherited from apply-group re0|re1, typical for multi-RE systems
config = dev.rpc.get_config(filter_xml=etree.XML(''' <configuration> <system> <host-name/> </system> </configuration>'''), options={'database':'committed','inherit':'inherit'})
- Parameters:
model (str) – Can provide yang model openconfig/custom/ietf. When model is True and filter_xml is None, xml is enclosed under <data> so that we get junos as well as other model configurations
namespace (str) – User can have their own defined namespace in the custom yang models, In such cases they need to provide that namespace so that it can be used to fetch yang modeled configs
remove_ns (bool) – remove namespaces, if value assigned is False, function will return xml with namespaces. The same xml returned can be loaded back to devices. This comes handy in case of yang based configs
dev.rpc.get_config(filter_xml='bgp', model='openconfig', remove_ns=False)
- load_config(contents, ignore_warning=False, **options)[source]¶
loads :contents: onto the Junos device, does not commit the change.
- Parameters:
ignore_warning –
A boolean, string or list of string. If the value is True, it will ignore all warnings regardless of the warning message. If the value is a string, it will ignore warning(s) if the message of each warning matches the string. If the value is a list of strings, ignore warning(s) if the message of each warning matches at least one of the strings in the list.
For example:
dev.rpc.load_config(cnf, ignore_warning=True) dev.rpc.load_config(cnf, ignore_warning='vrrp subsystem not running') dev.rpc.load_config(cnf, ignore_warning=['vrrp subsystem not running', 'statement not found']) dev.rpc.load_config(cnf, ignore_warning='statement not found')
Note
When the value of ignore_warning is a string, or list of strings, the string is actually used as a case-insensitive regular expression pattern. If the string contains only alpha-numeric characters, as shown in the above examples, this results in a case-insensitive substring match. However, any regular expression pattern supported by the re library may be used for more complicated match conditions.
- 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
- format=’json’, then :contents: is a string containing Junos
configuration in json format
url=’path’, then :contents: is a None
<otherwise> :contents: is XML structure