def read_confd(self): rsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) ns = dhcpd_ns.ns cdb.connect(rsock, cdb.READ_SOCKET, '127.0.0.1', _confd.CONFD_PORT, '/') cdb.start_session(rsock, cdb.RUNNING) cdb.set_namespace(rsock, ns.hash) default_lease_time = cdb.get(rsock, "/dhcp/defaultLeaseTime") max_lease_time = cdb.get(rsock, "/dhcp/maxLeaseTime") log_facility = cdb.get(rsock, "/dhcp/logFacility") if log_facility == ns.dhcpd_kern: log_facility_string = "log-facility kern" if log_facility == ns. dhcpd_mail: log_facility_string = "log-facility mail" if log_facility == ns.dhcpd_local7: log_facility_string = "log-facility local7" self.fp = open("dhcpd.conf.tmp", "w") self.fp.write("default-lease-time {dlt}\n" "max-lease-time {mlt}\n" "{lfs}\n".format(dlt=duration2secs(default_lease_time), mlt=duration2secs(max_lease_time), lfs=log_facility_string)) sub_nets = cdb.num_instances( rsock, "/dhcp/SubNets/subNet") for i in range(0, sub_nets): cdb.cd(rsock, "/dhcp/SubNets/subNet[{index}]".format(index=i)) self.do_subnet(rsock) shared_networks = cdb.num_instances( rsock, "/dhcp/SharedNetworks/sharedNetwork" ) for i in range(0, shared_networks): sh_net = "/dhcp/SharedNetworks/sharedNetwork[{0}]".format(str(i)) network_name = cdb.get( rsock, sh_net + "/name" ) self.fp.write("shared-network {0} {{\n".format(str(network_name))) m = cdb.num_instances( rsock, sh_net + "/SubNets/subNet") for j in range(0, m): cdb.pushd(rsock, sh_net + "/SubNets/subNet[{0}]".format(str(j))) self.do_subnet(rsock) cdb.popd(rsock) self.fp.write("}\n") self.fp.close() return cdb.close(rsock)
def cb_action(self, uinfo, name, kp, input, output): sub_points = [] cds = socket.socket() cdb.connect(cds, cdb.DATA_SOCKET, CONFD_ADDR, confd.CONFD_PORT) cdb.start_session(cds, cdb.OPERATIONAL) if input.all.exists() or len(input.server) == 0: num = cdb.num_instances(cds, SERVER_PATH) for i in range(num): sub_point = int( cdb.get(cds, "{}[{}]/subscription-id".format(SERVER_PATH, i))) sub_points.append(sub_point) cdb.end_session(cds) cdb.trigger_subscriptions(cds, sub_points) output.result = "Synchronized the configuration with all NETCONF servers" else: server_names = input.server for server_name in server_names: sub_point = int( cdb.get( cds, "{0}{{{1}}}/subscription-id".format( SERVER_PATH, server_name))) sub_points.append(sub_point) cdb.end_session(cds) cdb.trigger_subscriptions(cds, sub_points) output.result = "Synchronized the configuration with {} NETCONF servers".format( ', '.join(server_names)) cds.close()
def __init__(self, prio=100, path='/'): self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) self.path = path self.prio = prio cdb.connect(self.sock, cdb.SUBSCRIPTION_SOCKET, '127.0.0.1', _confd.CONFD_PORT, self.path) cdb.subscribe(self.sock, self.prio, dhcpd_ns.ns.hash, self.path) cdb.subscribe_done(self.sock) print("Subscribed to {path}".format(path=self.path))
def run(): log.info("==>") # In C we use confd_init() which sets the debug-level, but for Python the # call to confd_init() is done when we do 'import confd'. # Therefore we need to set the ConfD debug level here (if we want it to be # different from the default debug level - CONFD_SILENT): _confd.set_debug(confd_debug_level, sys.stderr) subsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) # maapi socket for load schemas maapisock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) maapi.connect(maapisock, CONFD_ADDR, _confd.CONFD_PORT) maapi.load_schemas(maapisock) cdb.connect(subsock, cdb.SUBSCRIPTION_SOCKET, CONFD_ADDR, _confd.CONFD_PORT) spoint = cdb.subscribe(subsock, 3, user_folders_ns.ns.hash, ROOT_PATH) cdb.subscribe_done(subsock) log.debug("Subscribed to path %s spoint=%i" % (ROOT_PATH, spoint)) log.info("CDB subscriber initialized!") try: _r = [subsock] _w = [] _e = [] log.debug("subscok connected, starting ConfD loop") while True: (r, w, e) = select.select(_r, _w, _e, 1) for rs in r: log.debug("rs.fileno=%i subscok.fileno=%i" % ( rs.fileno(), subsock.fileno())) if rs.fileno() == subsock.fileno(): log.debug("subsock triggered") try: process_subscriptions(spoint, subsock) except _confd.error.Error as e: if e.confd_errno is not _confd.ERR_EXTERNAL: raise e except KeyboardInterrupt: print("\nCtrl-C pressed\n") finally: subsock.close() log.info("<==")
def send_netconf_operation(op, sid, q): crs = socket.socket() cdb.connect(crs, cdb.DATA_SOCKET, CONFD_ADDR, confd.CONFD_PORT) cds = socket.socket() cdb.connect(cds, cdb.DATA_SOCKET, CONFD_ADDR, confd.CONFD_PORT) cdb.start_session2(crs, cdb.RUNNING, 0) cdb.start_session(cds, cdb.OPERATIONAL) num = cdb.num_instances(crs, SERVER_PATH) for i in range(num): id = int(cdb.get(cds, "{}[{}]/subscription-id".format(SERVER_PATH, i))) if id == sid: break if id != sid: cds.close() crs.close() print("Subscription ID: {}".format(sid)) return ipaddr = str(cdb.get(crs, "{}[{}]/remote-address".format(SERVER_PATH, i))) port = str(cdb.get(crs, "{}[{}]/remote-port".format(SERVER_PATH, i))) user = str(cdb.get(crs, "{}[{}]/username".format(SERVER_PATH, i))) passwd = str(cdb.get(crs, "{}[{}]/password".format(SERVER_PATH, i))) proc = subprocess.Popen( "netconf-console --user={} --password={} --host={} --port={} -".format( user, passwd, ipaddr, port), shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) cmd = op output, errors = proc.communicate(input=cmd.encode('utf-8')) proc.stdout.close() proc.stderr.close() reply = output.decode() errstr = errors.decode() if reply.find('<rpc-error>') != -1 or errstr != "": q.put((reply, errstr)) cds.close() crs.close()
def handle_netconf_server_list(lock): crs = socket.socket() cdb.connect(crs, cdb.DATA_SOCKET, CONFD_ADDR, confd.CONFD_PORT) cds = socket.socket() cdb.connect(cds, cdb.DATA_SOCKET, CONFD_ADDR, confd.CONFD_PORT) css = socket.socket() cdb.connect(css, cdb.SUBSCRIPTION_SOCKET, CONFD_ADDR, confd.PORT) cdb.start_session2(crs, cdb.RUNNING, lock) cdb.start_session(cds, cdb.OPERATIONAL) num = cdb.num_instances(crs, SERVER_PATH) for i in range(num): sub_path = str( cdb.get(crs, "{}[{}]/subscription-path".format(SERVER_PATH, i))) sub_prio = int( cdb.get(crs, "{}[{}]/subscription-priority".format(SERVER_PATH, i))) sid = cdb.subscribe2(css, cdb.SUB_RUNNING_TWOPHASE, cdb.SUB_WANT_ABORT_ON_ABORT, sub_prio, 0, sub_path) cdb.set_elem(cds, str(sid), "{}[{}]/subscription-id".format(SERVER_PATH, i)) cds.close() server_prio = int(cdb.get(crs, "{}/server-priority".format(CLIENT_PATH))) server_sid = cdb.subscribe(css, server_prio, 0, SERVER_PATH) crs.close() cdb.subscribe_done(css) return css, server_sid
def run(): """ Main subscriber thread execution. """ confd_addr = '127.0.0.1' confd_port = _confd.PORT _confd.set_debug(_confd.SILENT, sys.stderr) sub_path = "/system/ip/route" # MAAPI socket to load schemas for a nicer print() calls maapi_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) maapi.connect(maapi_sock, confd_addr, confd_port, sub_path) maapi.load_schemas(maapi_sock) # socket for subscription data iteration data_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) cdb.connect(data_sock, cdb.DATA_SOCKET, confd_addr, confd_port, sub_path) # tailf:cdb-oper subscription socket oper_sub_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) cdb.connect(oper_sub_sock, cdb.SUBSCRIPTION_SOCKET, confd_addr, confd_port, sub_path) oper_sub_point = cdb.oper_subscribe(oper_sub_sock, routes_ns.ns.hash, sub_path) cdb.subscribe_done(oper_sub_sock) try: read_list = [oper_sub_sock] write_list = [] error_list = [] print('entering poll loop') while True: read_socks = select.select(read_list, write_list, error_list, 1)[0] for rs in read_socks: # process only our cdb-oper subscription socket here... if rs.fileno() is not oper_sub_sock.fileno(): continue try: sub_points = cdb.read_subscription_socket(oper_sub_sock) for s in sub_points: if s != oper_sub_point: continue print("CDB operational subscription point triggered") cdb.start_session(data_sock, cdb.OPERATIONAL) cdb.set_namespace(data_sock, routes_ns.ns.hash) cdb.diff_iterate(oper_sub_sock, oper_sub_point, iterate_changes, 0, None) cdb.end_session(data_sock) cdb.sync_subscription_socket(oper_sub_sock, cdb.DONE_OPERATIONAL) except _confd.error.Error as e: if e.confd_errno is not _confd.ERR_EXTERNAL: raise e except KeyboardInterrupt: print("\nCtrl-C pressed\n") finally: data_sock.close() oper_sub_sock.close()
def run(): log.info("==>") # In C we use confd_init() which sets the debug-level, but for Python the # call to confd_init() is done when we do 'import confd'. # Therefore we need to set the ConfD debug level here (if we want it to be # different from the default debug level - CONFD_SILENT): _confd.set_debug(confd_debug_level, sys.stderr) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) subsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) # maapi socket for load schemas maapisock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) maapi.connect(maapisock, CONFD_ADDR, _confd.CONFD_PORT) maapi.load_schemas(maapisock) cdb.connect(sock, cdb.DATA_SOCKET, CONFD_ADDR, _confd.CONFD_PORT) sub_path = "/root/NodeB/RFHead" cdb.connect(subsock, cdb.SUBSCRIPTION_SOCKET, CONFD_ADDR, _confd.CONFD_PORT) spoint = cdb.subscribe(subsock, 3, root_ns.ns.hash, sub_path) cdb.subscribe_done(subsock) log.debug("Subscribed to path %s spoint=%i" % (sub_path, spoint)) read_db(sock) try: _r = [subsock] _w = [] _e = [] log.debug("subscok connected, starting ConfD loop") while (True): (r, w, e) = select.select(_r, _w, _e, 1) # log.debug("select triggered r=%r" % r) for rs in r: log.debug("rs.fileno=%i subscok.fileno=%i" % (rs.fileno(), subsock.fileno())) if rs.fileno() == subsock.fileno(): log.debug("subsock triggered") try: sub_points = cdb.read_subscription_socket(subsock) for s in sub_points: if s == spoint: log.debug("our spoint=%i triggered" % spoint) cdb.start_session(sock, cdb.RUNNING) cdb.set_namespace(sock, root_ns.ns.hash) cdb.diff_iterate(subsock, spoint, iter, _confd.ITER_WANT_PREV, sock) cdb.end_session(sock) # variant with diff_match not yet available # in python cdb.sync_subscription_socket( subsock, cdb.DONE_PRIORITY) except (_confd.error.Error) as e: if e.confd_errno is not _confd.ERR_EXTERNAL: raise e except KeyboardInterrupt: print("\nCtrl-C pressed\n") finally: sock.close() subsock.close() log.info("<==")