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 do_subnet(self, rsock): net = cdb.get(rsock, "net") mask = cdb.get(rsock, "mask") self.fp.write("subnet {net} netmask {netmask} {{\n".format( net=str(net), netmask=str(mask))) if cdb.exists(rsock, "range"): self.fp.write(" range ") dynamic_bootp = cdb.get(rsock, "range/dynamicBootP") if dynamic_bootp: self.fp.write(" dynamic-bootp ") low_addr = cdb.get(rsock, "range/lowAddr") hi_addr = cdb.get(rsock, "range/hiAddr") self.fp.write(" {low} {high} \n".format( low=str(low_addr), high=str(hi_addr) )) if cdb.exists(rsock, "routers"): routers = cdb.get(rsock, "routers") comma_routers = str(routers).replace(" ", ",") self.fp.write(" option routers {0}\n".format(comma_routers)) mlt = cdb.get(rsock, "maxLeaseTime") self.fp.write(" max-lease-time {0}\n}};\n".format(duration2secs(mlt)))
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 read_head(cdbsock, headkey): log.debug("==> cdbsock.fileno()=%d headkey=%d" % (cdbsock.fileno(), headkey)) hp = rfheads.get(headkey) if hp is None: hp = Rfhead() rfheads[headkey] = hp log.debug("Created new hp=%s" % hp) cdb.cd(cdbsock, "/root/NodeB/RFHead{%d}" % headkey) hp.sector_id = str(cdb.get(cdbsock, "SECTORID_ID")) hp.children.clear() log.debug("hp=%s", hp) n = cdb.num_instances(cdbsock, "Child") log.debug("number of child instances n=%d" % n) for i in range(n): dn = int(cdb.get(cdbsock, "Child[%d]/cdn" % i)) childattr = str(cdb.get(cdbsock, "Child[%d]/childAttr" % i)) hp.children[dn] = childattr log.debug("<== processed hp=%s" % hp)
def read_db(cdbsock): log.debug("==> cdbsock.fileno()=%d" % cdbsock.fileno()) cdb.start_session(cdbsock, cdb.RUNNING) cdb.set_namespace(cdbsock, root_ns.ns.hash) n = cdb.num_instances(cdbsock, "/root/NodeB/RFHead") for k, v in rfheads.items(): v.children.clear() rfheads.clear() for i in range(n): key = int(cdb.get(cdbsock, "/root/NodeB/RFHead[%d]/dn" % i)) read_head(cdbsock, key) cdb.end_session(cdbsock) log.debug("<== rfheads=%s" % _str_rfh(rfheads))