def __init__(self, *args, **kwargs): ''' __init__ instantiates a single connection instance. ''' # set defaults kwargs.setdefault('timeout', 30) # instanciate BaseConnection # (could use super...) BaseConnection.__init__(self, *args, **kwargs) # shortwire Ncclient device handling portion # and create just the DeviceHandler device_handler = DefaultDeviceHandler() # create the session instance session = transport.SSHSession(device_handler) # load known_hosts file (if available) session.load_known_hosts() # instanciate ncclient Manager # (can't use super due to mro change) manager.Manager.__init__(self, session = session, device_handler = device_handler, timeout = self.timeout)
def test_rawrpc(self): from ncclient.operations.retrieve import GetReply h = DefaultDeviceHandler() self.rawrpc = yang.connector.netconf.RawRPC( session=transport.SSHSession(h), device_handler=h) self.rawrpc._event = MyEvent() self.rawrpc._session = MySSHSession() reply_raw = ''' <rpc-reply message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" > <data> <native xmlns="http://cisco.com/ns/yang/ned/ios"> <version>16.3</version> </native> </data> </rpc-reply> ''' self.rawrpc._reply = GetReply(reply_raw) r = ''' <rpc message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> <get> <filter> <native xmlns="http://cisco.com/ns/yang/ned/ios"> <version> </version> </native> </filter> </get> </rpc> ''' generated_value = self.rawrpc._request(r).xml expected_value = ''' <rpc-reply message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" > <data> <native xmlns="http://cisco.com/ns/yang/ned/ios"> <version>16.3</version> </native> </data> </rpc-reply> ''' self.assertEqual(generated_value, expected_value)
def connect(*args, **kwargs): """ Initialize a :class:`ModelDevice` over the SSH transport. For documentation of arguments see :meth:`ncclient.transport.SSHSession.connect`. The underlying :class:`ncclient.transport.SSHSession` is created with :data:`CAPABILITIES`. It is first instructed to :meth:`~ncclient.transport.SSHSession.load_known_hosts` and then all the provided arguments are passed directly to its implementation of :meth:`~ncclient.transport.SSHSession.connect`. """ device_handler = DefaultDeviceHandler() session = transport.SSHSession(device_handler) if "hostkey_verify" not in kwargs or kwargs["hostkey_verify"]: session.load_known_hosts() try: session.connect(*args, **kwargs) except Exception as ex: if session.transport: session.close() raise return ModelDevice(session, device_handler, **kwargs)
def setUp(self): self.obj = DefaultDeviceHandler()