How to use ncclient - 10 common examples

To help you get started, we’ve selected a few ncclient examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ncclient / ncclient / test / unit / operations / test_lock.py View on Github external
def test_lock_context_exit(self, mock_request, mock_session):
        session = ncclient.transport.SSHSession(self.device_handler)
        obj = LockContext(session, self.device_handler, "running")
        self.assertFalse(obj.__exit__())
        node = new_ele("unlock")
        sub_ele(sub_ele(node, "target"), "running")
        xml = ElementTree.tostring(node)
        call = mock_request.call_args_list[0][0][0]
        call = ElementTree.tostring(call)
        self.assertEqual(call, xml)
github ncclient / ncclient / test / unit / operations / test_edit.py View on Github external
def test_commit(self, mock_request):
        session = ncclient.transport.SSHSession(self.device_handler)
        session._server_capabilities = [':candidate', ":confirmed-commit"]
        obj = Commit(session, self.device_handler, raise_mode=RaiseMode.ALL)
        obj.request(confirmed=True, timeout="0")
        node = new_ele("commit")
        sub_ele(node, "confirmed")
        sub_ele(node, "confirm-timeout").text = "0"
        xml = ElementTree.tostring(node)
        call = mock_request.call_args_list[0][0][0]
        call = ElementTree.tostring(call)
        self.assertEqual(call, xml)
github ncclient / ncclient / test / unit / test_manager.py View on Github external
def test_connect_ssh_with_hostkey_ed25519(self, mock_ssh):
        hostkey = 'AAAAC3NzaC1lZDI1NTE5AAAAIIiHpGSf8fla6tCwLpwshvMGmUK+B/0v5CsRu+5v4uT7'
        manager.connect(host='host', hostkey=hostkey)
        mock_ssh.assert_called_once_with(host='host', hostkey=hostkey)
github ncclient / ncclient / test / unit / test_xml_.py View on Github external
def test_ncelement_reply_003(self):
        """test parse rpc_reply and find"""
        # read in reply1 contents
        with open(file_path, 'r') as f:
            reply = f.read()
        device_params = {'name': 'junos'}
        device_handler = manager.make_device_handler(device_params)
        transform_reply = device_handler.transform_reply()
        result = NCElement(reply, transform_reply)
        # find
        assert_equal(result.findtext(".//host-name"), "R1")
        assert_equal(result.find(".//host-name").tag, "host-name")
github ncclient / ncclient / test / unit / transport / test_parser.py View on Github external
def test_filter_xml_sax_on(self, mock_uuid4, mock_select, mock_session, mock_recv,
                              mock_close, mock_send, mock_send_ready, mock_connected):
        mock_send.return_value = True
        mock_send_ready.return_value = -1
        mock_uuid4.return_value = type('dummy', (), {'urn': "urn:uuid:e0a7abe3-fffa-11e5-b78e-b8e85604f858"})
        device_handler = manager.make_device_handler({'name': 'junos', 'use_filter': True})
        rpc = ''
        mock_recv.side_effect = self._read_file('get-software-information.xml')
        session = SSHSession(device_handler)
        session._connected = True
        session._channel = paramiko.Channel("c100")
        session.parser = session._device_handler.get_xml_parser(session)
        obj = ExecuteRpc(session, device_handler, raise_mode=RaiseMode.ALL)
        obj._filter_xml = ''
        session.run()
        resp = obj.request(rpc)._NCElement__doc[0]
        self.assertEqual(len(resp.xpath('multi-routing-engine-item/re-name')), 2)
        # as filter_xml is not having software-information, response wont contain it
        self.assertEqual(len(resp.xpath('multi-routing-engine-item/software-information')), 0)
github ncclient / ncclient / test / unit / transport / test_parser.py View on Github external
def test_filter_xml_sax_on_junos_rfc_compliant(self, mock_uuid4, mock_select, mock_session, mock_recv,
                              mock_close, mock_send, mock_send_ready, mock_connected):
        mock_send.return_value = True
        mock_send_ready.return_value = -1
        mock_uuid4.return_value = type('dummy', (), {'urn': "urn:uuid:e0a7abe3-fffa-11e5-b78e-b8e85604f858"})
        device_handler = manager.make_device_handler({'name': 'junos', 'use_filter': True})
        rpc = ''
        mock_recv.side_effect = self._read_file('get-software-information.xml')
        session = SSHSession(device_handler)
        session._connected = True
        session._channel = paramiko.Channel("c100")
        session.parser = session._device_handler.get_xml_parser(session)
        obj = ExecuteRpc(session, device_handler, raise_mode=RaiseMode.ALL)
        obj._filter_xml = ''
        session.run()
        resp = obj.request(rpc)._NCElement__doc[0]
        from lxml import etree
        print(resp)
        self.assertEqual(len(resp.xpath('multi-routing-engine-item/re-name')), 2)
        # as filter_xml is not having software-information, response wont contain it
        self.assertEqual(len(resp.xpath('multi-routing-engine-item/software-information')), 0)
github Juniper / py-junos-eznc / tests / unit / factory / test_optable.py View on Github external
def _mock_manager(self, *args, **kwargs):
        if kwargs:
            if args and ('normalize' in kwargs or 'filter_xml' in kwargs):
                return self._read_file(args[0].tag + '.xml')
            device_params = kwargs['device_params']
            device_handler = make_device_handler(device_params)
            session = SSHSession(device_handler)
            return Manager(session, device_handler)

        if args:
            return self._read_file(args[0].tag + '.xml')
github ncclient / ncclient / test / unit / operations / third_party / juniper / test_rpc.py View on Github external
def test_getconf(self, mock_request, mock_session):
        device_handler = manager.make_device_handler({'name': 'junos'})
        session = ncclient.transport.SSHSession(device_handler)
        obj = GetConfiguration(
            session,
            device_handler,
            raise_mode=RaiseMode.ALL)
        root_filter = new_ele('filter')
        config_filter = sub_ele(root_filter, 'configuration')
        system_filter = sub_ele(config_filter, 'system')
        obj.request(format='xml', filter=system_filter)
        node = new_ele('get-configuration', {'format': 'xml'})
        node.append(system_filter)
        call = mock_request.call_args_list[0][0][0]
        self.assertEqual(call.tag, node.tag)
        self.assertEqual(call.attrib, node.attrib)
github Juniper / py-junos-eznc / tests / unit / facts / test_ifd_style.py View on Github external
def _mock_manager_setup(self, *args, **kwargs):
        if kwargs:
            device_params = kwargs['device_params']
            device_handler = make_device_handler(device_params)
            session = SSHSession(device_handler)
            return Manager(session, device_handler)
github ncclient / ncclient / test / unit / operations / third_party / juniper / test_rpc.py View on Github external
def test_loadconf_text(self, mock_request, mock_session):
        device_handler = manager.make_device_handler({'name': 'junos'})
        session = ncclient.transport.SSHSession(device_handler)
        obj = LoadConfiguration(
            session,
            device_handler,
            raise_mode=RaiseMode.ALL)
        config = 'system { location floor 7; }'
        obj.request(format='text', action='merge', config=config)
        node = new_ele('load-configuration', {'format': 'text', 'action': 'merge'})
        sub_ele(node, 'configuration-text').text = config
        call = mock_request.call_args_list[0][0][0]
        self.assertEqual(call.tag, node.tag)
        self.assertEqual(call.attrib, node.attrib)