PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/source/manager.rst

http://ncclient.googlecode.com/
ReStructuredText | 319 lines | 205 code | 114 blank | 0 comment | 0 complexity | e01ee413be179d94d76a610015ef5732 MD5 | raw file
Possible License(s): Apache-2.0
  1. :mod:`~ncclient.manager` -- High-level API
  2. ==========================================
  3. .. module:: ncclient.manager
  4. :synopsis: High-level API
  5. .. data:: CAPABILITIES
  6. List of URI strings representing the client's capabilities. This is used during the initial
  7. capability exchange. Modify this if you need to announce some capability not already included.
  8. .. data:: OPERATIONS
  9. Dictionary of method names and corresponding `~ncclient.operations.RPC` classes. `Manager` uses
  10. this to lookup operations, e.g. "get_config" is mapped to `ncclient.operations.GetConfig`. It
  11. is thus possible to add additional operations to the `Manager` API.
  12. Factory functions
  13. -----------------
  14. A `Manager` instance is created using a factory function.
  15. .. function:: connect_ssh(host[, port=830, timeout=None, unknown_host_cb=default_unknown_host_cb, username=None, password, key_filename=None, allow_agent=True, look_for_keys=True])
  16. Initializes a NETCONF session over SSH, and creates a connected `Manager` instance. *host* must
  17. be specified, all the other arguments are optional and depend on the kind of host key
  18. verification and user authentication you want to complete.
  19. For the purpose of host key verification, on -NIX systems a user's :file:`~/.ssh/known_hosts`
  20. file is automatically considered. The *unknown_host_cb* argument specifies a callback that will
  21. be invoked when the server's host key cannot be verified. See
  22. :func:`~ncclient.transport.ssh.default_known_host_cb` for function signature.
  23. First, ``publickey`` authentication is attempted. If a specific *key_filename* is specified, it
  24. will be loaded and authentication attempted using it. If *allow_agent* is :const:`True` and an
  25. SSH agent is running, the keys provided by the agent will be tried. If *look_for_keys* is
  26. :const:`True`, keys in the :file:`~/.ssh/id_rsa` and :file:`~.ssh/id_dsa` will be tried. In case
  27. an encrypted key file is encountered, the *password* argument will be used as a decryption
  28. passphrase.
  29. If ``publickey`` authentication fails and the *password* argument has been supplied,
  30. ``password`` / ``keyboard-interactive`` SSH authentication will be attempted.
  31. :param host: hostname or address on which to connect
  32. :type host: `string`
  33. :param port: port on which to connect
  34. :type port: `int`
  35. :param timeout: timeout for socket connect
  36. :type timeout: `int`
  37. :param unknown_host_cb: optional; callback that is invoked when host key verification fails
  38. :type unknown_host_cb: `function`
  39. :param username: username to authenticate with, if not specified the username of the logged-in
  40. user is used
  41. :type username: `string`
  42. :param password: password for ``password`` authentication or passphrase for decrypting
  43. private key files
  44. :type password: `string`
  45. :param key_filename: location of a private key file on the file system
  46. :type key_filename: `string`
  47. :param allow_agent: whether to try connecting to SSH agent for keys
  48. :type allow_agent: `bool`
  49. :param look_for_keys: whether to look in usual locations for keys
  50. :type look_for_keys: `bool`
  51. :raises: :exc:`~ncclient.transport.SSHUnknownHostError`
  52. :raises: :exc:`~ncclient.transport.AuthenticationError`
  53. :rtype: `Manager`
  54. .. function:: connect()
  55. Same as :func:`connect_ssh`, since SSH is the default (and currently, the
  56. only) transport.
  57. Manager
  58. -------
  59. Exposes an API for RPC operations as method calls. The return type of these methods depends on
  60. whether we are is in :attr:`asynchronous or synchronous mode <ncclient.manager.Manager.async_mode>`.
  61. In synchronous mode replies are awaited and the corresponding `~ncclient.operations.RPCReply` object
  62. is returned. Depending on the :attr:`exception raising mode <ncclient.manager.Manager.raise_mode>`,
  63. an *rpc-error* in the reply may be raised as :exc:`RPCError` exceptions.
  64. However in asynchronous mode, operations return immediately with an `~ncclient.operations.RPC`
  65. object. Error handling and checking for whether a reply has been received must be dealt with
  66. manually. See the `~ncclient.operations.RPC` documentation for details.
  67. Note that in case of the *get* and *get-config* operations, the reply is an instance of
  68. `~ncclient.operations.GetReply` which exposes the additional attributes
  69. :attr:`~ncclient.operations.GetReply.data` (as `~xml.etree.ElementTree.Element`) and
  70. :attr:`~ncclient.operations.GetReply.data_xml` (as `string`), which are of primary interest in case
  71. of these operations.
  72. Presence of capabilities is verified to the extent possible, and you can expect a
  73. :exc:`~ncclient.operations.MissingCapabilityError` if something is amiss. In case of transport-layer
  74. errors, e.g. unexpected session close, :exc:`~ncclient.transport.TransportError` will be raised.
  75. .. class:: Manager
  76. For details on the expected behavior of the operations and their parameters
  77. refer to :rfc:`4741`.
  78. Manager instances are also context managers so you can use it like this::
  79. with manager.connect("host") as m:
  80. # do your stuff
  81. ... or like this::
  82. m = manager.connect("host")
  83. try:
  84. # do your stuff
  85. finally:
  86. m.close()
  87. .. method:: get_config(source[, filter=None])
  88. Retrieve all or part of a specified configuration.
  89. :param source: name of the configuration datastore being queried
  90. :type source: `string`
  91. :param filter: portions of the device configuration to retrieve (by default entire configuration is retrieved)
  92. :type filter: :ref:`filter_params`
  93. .. method:: edit_config(target, config[, default_operation=None, test_option=None, error_option=None])
  94. Loads all or part of a specified configuration to the specified target configuration.
  95. The ``"rollback-on-error"`` *error_option* depends on the ``:rollback-on-error`` capability.
  96. :param target: name of the configuration datastore being edited
  97. :type target: `string`
  98. :param config: configuration (must be rooted in *<config> .. </config>*)
  99. :type config: `string` or `~xml.etree.ElementTree.Element`
  100. :param default_operation: one of { ``"merge"``, ``"replace"``, or ``"none"`` }
  101. :type default_operation: `string`
  102. :param test_option: one of { ``"test_then_set"``, ``"set"`` }
  103. :type test_option: `string`
  104. :param error_option: one of { ``"stop-on-error"``, ``"continue-on-error"``, ``"rollback-on-error"`` }
  105. :type error_option: `string`
  106. .. method:: copy_config(source, target)
  107. Create or replace an entire configuration datastore with the contents of another complete
  108. configuration datastore.
  109. :param source: configuration datastore to use as the source of the copy operation or *<config>* element containing the configuration subtree to copy
  110. :type source: :ref:`srctarget_params`
  111. :param target: configuration datastore to use as the destination of the copy operation
  112. :type target: :ref:`srctarget_params`
  113. .. method:: delete_config(target)
  114. Delete a configuration datastore.
  115. :param target: name or URL of configuration datastore to delete
  116. :type: :ref:`srctarget_params`
  117. .. method:: lock(target)
  118. Allows the client to lock the configuration system of a device.
  119. :param target: name of the configuration datastore to lock
  120. :type target: `string`
  121. .. method:: unlock(target)
  122. Release a configuration lock, previously obtained with the
  123. :meth:`~ncclient.manager.Manager.lock` operation.
  124. :param target: name of the configuration datastore to unlock
  125. :type target: `string`
  126. .. method:: locked(target)
  127. Returns a context manager for a lock on a datastore, e.g.::
  128. with m.locked("running"):
  129. # do your stuff
  130. ... instead of::
  131. m.lock("running")
  132. try:
  133. # do your stuff
  134. finally:
  135. m.unlock("running")
  136. :param target: name of configuration datastore to lock
  137. :type target: `string`
  138. :rtype: `~ncclient.operations.LockContext`
  139. .. method:: get([filter=None])
  140. Retrieve running configuration and device state information.
  141. :param filter: portions of the device configuration to retrieve (by default entire configuration is retrieved)
  142. :type filter: :ref:`filter_params`
  143. .. method:: close_session()
  144. Request graceful termination of the NETCONF session, and also close the transport.
  145. .. method:: kill_session(session_id)
  146. Force the termination of a NETCONF session (not the current one!).
  147. :param session_id: session identifier of the NETCONF session to be terminated
  148. :type session_id: `string`
  149. .. method:: commit([confirmed=False, timeout=None])
  150. Commit the candidate configuration as the device's new current configuration. Depends on the
  151. *:candidate* capability.
  152. A confirmed commit (i.e. if *confirmed* is :const:`True`) is reverted if there is no
  153. followup commit within the *timeout* interval. If no timeout is specified the confirm
  154. timeout defaults to 600 seconds (10 minutes). A confirming commit may have the *confirmed*
  155. parameter but this is not required. Depends on the *:confirmed-commit* capability.
  156. :param confirmed: whether this is a confirmed commit
  157. :type confirmed: `bool`
  158. :param timeout: confirm timeout in seconds
  159. :type timeout: `int`
  160. .. method:: discard_changes()
  161. Revert the candidate configuration to the currently running configuration. Any uncommitted
  162. changes are discarded.
  163. .. method:: validate(source)
  164. Validate the contents of the specified configuration.
  165. :param source: name of the configuration datastore being validated or *<config>* element containing the configuration subtree to be validated
  166. :type source: :ref:`srctarget_params`
  167. .. attribute:: async_mode
  168. Specify whether operations are executed asynchronously (:const:`True`)
  169. or synchronously (:const:`False`) (the default).
  170. .. attribute:: raise_mode
  171. Specify which errors are raised as :exc:`~ncclient.operations.RPCError` exceptions.
  172. Valid values:
  173. * ``"all"`` -- any kind of *rpc-error* (error or warning)
  174. * ``"errors"`` -- where the *error-type* attribute says it is an error
  175. * ``"none"`` -- neither
  176. .. attribute:: client_capabilities
  177. `~ncclient.capabilities.Capabilities` object representing the client's capabilities.
  178. .. attribute:: server_capabilities
  179. `~ncclient.capabilities.Capabilities` object representing the server's capabilities.
  180. .. attribute:: session_id
  181. *session-id* assigned by the NETCONF server.
  182. .. attribute:: connected
  183. Bolean value indicating whether currently connected to the NETCONF server.
  184. Special kinds of parameters
  185. ---------------------------
  186. Some parameters can take on different types to keep the interface simple.
  187. .. _srctarget_params:
  188. Source and target parameters
  189. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  190. Where an method takes a *source* or *target* argument, usually a datastore name or URL is expected.
  191. The latter depends on the ``:url`` capability and on whether the specific URL scheme is supported.
  192. Either must be specified as a `string`. For example, ``"running"``,
  193. ``"ftp://user:pass@host/config"``.
  194. If the source may be a *<config>* element, e.g. as allowed for the *validate* RPC, it can also be
  195. specified as an XML string or an `~xml.etree.ElementTree.Element` object.
  196. .. _filter_params:
  197. Filter parameters
  198. ^^^^^^^^^^^^^^^^^
  199. Where a method takes a *filter* argument, it can take on the following types:
  200. * A ``tuple`` of *(type, criteria)*.
  201. Here *type* has to be one of ``"xpath"`` or ``"subtree"``.
  202. * For ``"xpath"`` the *criteria* should be a `string` containing the XPath expression.
  203. * For ``"subtree"`` the *criteria* should be an XML string or an
  204. `~xml.etree.ElementTree.Element` object containing the criteria.
  205. * A *<filter>* element as an XML string or an `~xml.etree.ElementTree.Element` object.