def perform(self): self.debug("config_record_state() with device {0}".format( self.dev_name)) state_name = self.state_name self.debug("incl_rollbacks=" + str(self.include_rollbacks)) try: ## list_rollbacks() returns one less rollback than the second argument, ## i.e. send 2 to get 1 rollback. Therefore the +1 rollbacks = maapi.list_rollbacks(self.msocket, int(self.include_rollbacks) + 1) ## rollbacks are returned 'most recent first', i.e. reverse chronological order except: rollbacks = [] self.debug("rollbacks=" + str([r.fixed_nr for r in rollbacks])) maapi.attach2(self.msocket, 0, 0, self.uinfo.actx_thandle) index = 0 state_filenames = [] for rb in [None] + rollbacks: if None == rb: self.debug("Recording current transaction state") else: self.debug("Recording rollback" + str(rb.fixed_nr)) self.debug("Recording rollback" + str(rb.nr)) maapi.load_rollback(self.msocket, self.uinfo.actx_thandle, 3) #rb.nr) save_id = maapi.save_config( self.msocket, self.uinfo.actx_thandle, maapi.CONFIG_J, "/ncs:devices/device{" + self.dev_name + "}/config") state_name_index = state_name if index > 0: state_name_index = state_name + "-" + str(index) state_filename = self.states_dir + "/" + self.state_name_to_filename( state_name_index, self.dev_name) with file(state_filename, "w") as state_file: try: ssocket = socket.socket() _ncs.stream_connect(sock=ssocket, id=save_id, flags=0, ip='127.0.0.1', port=_ncs.NCS_PORT) while True: config_data = ssocket.recv(4096) if not config_data: break state_file.write(str(config_data)) self.debug("Data: " + str(config_data)) finally: ssocket.close() state_filenames += [state_name_index] ##maapi.save_config_result(sock, id) -> None index += 1 maapi.revert(self.msocket, self.uinfo.actx_thandle) return {'success': "Recorded states " + str(state_filenames)}
def read_config(m, th, path): dev_flags = (_ncs.maapi.CONFIG_JSON) c_id = _ncs.maapi.save_config(m, th, dev_flags, path) c_sock = socket.socket() (ncsip, ncsport) = m.getpeername() _ncs.stream_connect(c_sock, c_id, 0, ncsip, ncsport) data = recv_all_and_close(c_sock, c_id) return data
def cb_action(self, uinfo, name, kp, input, output): self.log.info('action name: ', name) with ncs.maapi.single_write_trans(uinfo.username, 'python') as trans: device = ncs.maagic.get_node(trans, kp) self.log.info(device.device_type) if (device.device_type.ne_type == 'cli' and (device.device_type.cli.ned_id.startswith('cisco-iosxr') or device.device_type.cli.ned_id.startswith('cisco-staros')) and (input.format == 'cli' or not input.format)): output.format = 'cli' format_flags = maapi.CONFIG_C elif (device.device_type.ne_type == 'cli' and not input.format or input.format == 'cli'): output.format = 'cli' format_flags = maapi.CONFIG_C_IOS elif (device.device_type.ne_type == 'netconf' and device.device_type.netconf.ned_id.startswith('juniper-junos') and not input.format or input.format == 'curly-braces'): output.format = 'curly-braces' format_flags = maapi.CONFIG_J elif input.format == 'json': output.format = 'json' format_flags = maapi.CONFIG_JSON elif input.format == 'yaml': output.format = 'yaml' format_flags = maapi.CONFIG_JSON else: output.format = 'xml' format_flags = maapi.CONFIG_XML_PRETTY if input.service_meta_data: format_flags |= maapi.CONFIG_WITH_SERVICE_META config_id = maapi.save_config(trans.maapi.msock, trans.th, format_flags, str(kp) + '/config') sock = socket.socket() # pylint: disable=no-member _ncs.stream_connect(sock, config_id, 0, '127.0.0.1', _ncs.NCS_PORT) config_bytes = b'' while 1: data = sock.recv(1024) if not data: break config_bytes += data sock.close() if input.format == 'yaml': json_config = json.loads(config_bytes) output.config = yaml.safe_dump(json_config, default_flow_style=False) else: output.config = config_bytes.decode('utf-8')
def save(self, target: str) -> None: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sck, \ open(target, 'wb') as out: path = self.device._path + '/config' sid = self.device._backend.save_config(_ncs.maapi.CONFIG_XML, path) _ncs.stream_connect(sck, sid, 0, '127.0.0.1', _ncs.PORT) while True: data = sck.recv(1024) if len(data) <= 0: break out.write(data)
def create_template(cmds, username, nedid): ''' Find all active CLI NED Ids ''' with ncs.maapi.Maapi() as m: with ncs.maapi.Session(m, username, 'system'): with m.start_write_trans() as trans: path = '/ncs:devices/ncs:device{TEMP1-DEVICE}/config' root = ncs.maagic.get_root(trans) tempd = root.ncs__devices.ncs__device.create( 'TEMP1-DEVICE') tempd.device_type.cli.ned_id = nedid flags = _ncs.maapi.CONFIG_C_IOS if 'xr' in nedid: flags = _ncs.maapi.CONFIG_C try: result = _ncs.maapi.load_config_cmds(m.msock, trans.th, flags | \ _ncs.maapi.CONFIG_REPLACE | \ _ncs.maapi.CONFIG_CONTINUE_ON_ERROR, cmds, path) c_id = _ncs.maapi.save_config(m.msock, trans.th, flags | \ _ncs.maapi.CONFIG_XML_PRETTY, path) c_sock = socket.socket() _ncs.stream_connect(c_sock, c_id, 0, '127.0.0.1', _ncs.NCS_PORT) data = NtoolTemplate.recv_all_and_close(c_sock, c_id) template = str(data) template = template.replace("<device>\n <name>TEMP1-DEVICE</name>", "<template>\n <name>TEMP-NAME</name>\n" + \ " <ned-id>\n" + \ " <id>" + nedid + "</id>") template = template.replace( "</device>\n </devices>", " </ned-id>\n </template>\n </devices>") return template except Exception as e: print("Exception creating template: %s" % e) return e
def save_config(self, trans, config_type, path): save_id = trans.save_config(config_type, path) try: ssocket = socket.socket() _ncs.stream_connect(sock=ssocket, id=save_id, flags=0, ip='127.0.0.1', port=_ncs.PORT) while True: config_data = ssocket.recv(4096) if not config_data: return yield config_data self.log.debug("Data: " + str(config_data)) finally: ssocket.close()
def save_to_git(trans, path_and_file, kp): m = ncs.maapi.Maapi() id = trans.save_config(m.CONFIG_XML_PRETTY, str(kp)) with open(path_and_file, "w+") as service_file: try: s = socket.socket() _ncs.stream_connect(s, id, 0, ip='127.0.0.1', port=_ncs.NCS_PORT) data = '' while True: config_data = s.recv(4096) if not config_data: break service_file.write(config_data.decode('utf-8')) data += str(config_data) return data finally: s.close()