def get_device_infor(self, device_name=None): """ @param device_name: name of interface in host @return: return a dict with key: DNS,IP,MTU,MAC,netmask,gateway,network, etc. """ if self._hypervisor_handler is None: self._hypervisor_handler = self.get_handler() try: device_dom = self._hypervisor_handler.interfaceLookupByName( device_name) except libvirtError as error: log.error("Exception when get device infor: %s", error) return {} default_infor = {} device_tree = xmlEtree.fromstring(device_dom.XMLDesc()) ip_element = device_tree.find("protocol[@family='ipv4']/ip") if ip_element is not None: prefix, ip = ip_element.attrib.get( 'prefix'), ip_element.attrib.get('address', None) default_infor.setdefault('IP', ip) default_infor.setdefault('netmask', str(IPv4Address._make_netmask(prefix)[0])) else: default_infor.setdefault('IP', None) default_infor.setdefault('netmask', None) default_infor.setdefault('device', device_name) default_infor.setdefault('DNS', None) default_infor.setdefault('MAC', device_dom.MACString()) default_infor.setdefault('gateway', None) return default_infor
def ip_addresses(self): '''returns a list of tuples containing: interface, ip address, netmask, and mask length''' from ipaddress import IPv4Address as addr ip_int_list = [] try: self.intf = self.json["ietf-interfaces:interface"]["name"] self.ip_address = self.json["ietf-interfaces:interface"][ "ietf-ip:ipv4"]["address"][0]["ip"] self.netmask = self.json["ietf-interfaces:interface"][ "ietf-ip:ipv4"]["address"][0]["netmask"] self.maskbits = addr._make_netmask(self.netmask)[1] ip_int_list.append( (self.intf, self.ip_address, self.netmask, self.maskbits)) except: for i in self.json['ietf-interfaces:interfaces']["interface"]: self.intf = i['name'] self.ip_address = i['ietf-ip:ipv4']['address'][0]['ip'] self.netmask = i["ietf-ip:ipv4"]["address"][0]["netmask"] self.maskbits = addr._make_netmask(self.netmask)[1] ip_int_list.append( (self.intf, self.ip_address, self.netmask, self.maskbits)) return ip_int_list
)) self.logger.debug( f"Sent out DHCP Request message to {dhcp_packet_rx.dhcp_srv_id}") # Wait for DHCP Ack if not (packet := socket.receive_from(timeout=5)): self.logger.warning("Timeout waiting for DHCP Ack message") return None, None dhcp_packet_rx = ps_dhcp.DhcpPacket(packet.raw_data) if dhcp_packet_rx.dhcp_msg_type != ps_dhcp.DHCP_ACK: self.logger.warning("Didn't receive DHCP Offer message") socket.close() return None, None self.logger.debug( f"Received DHCP Offer from {dhcp_packet_rx.dhcp_srv_id}" + f"IP: {dhcp_packet_rx.dhcp_yiaddr}, Mask: {dhcp_packet_rx.dhcp_subnet_mask}, Router: {dhcp_packet_rx.dhcp_router}" + f"DNS: {dhcp_packet_rx.dhcp_dns}, Domain: {dhcp_packet_rx.dhcp_domain_name}" ) socket.close() return ( IPv4Interface( str(dhcp_packet_rx.dhcp_yiaddr) + "/" + str( IPv4Address._make_netmask( str(dhcp_packet_rx.dhcp_subnet_mask))[1])), dhcp_packet_rx.dhcp_router[0], )
dhcp_srv_id=dhcp_srv_id, dhcp_req_ipv4_addr=dhcp_yiaddr, dhcp_param_req_list=b"\x01\x1c\x02\x03\x0f\x06\x77\x0c\x2c\x2f\x1a\x79\x2a", dhcp_host_name="PyTCP", ) ) self.logger.debug(f"Sent out DHCP Request message to {dhcp_packet_rx.dhcp_srv_id}") # Wait for DHCP Ack if not (packet := socket.receive_from(timeout=5)): self.logger.warning("Timeout waiting for DHCP Ack message") return None, None dhcp_packet_rx = ps_dhcp.DhcpPacket(packet.raw_data) if dhcp_packet_rx.dhcp_msg_type != ps_dhcp.DHCP_ACK: self.logger.warning("Didn't receive DHCP Offer message") socket.close() return None, None self.logger.debug( f"Received DHCP Offer from {dhcp_packet_rx.dhcp_srv_id}" + f"IP: {dhcp_packet_rx.dhcp_yiaddr}, Mask: {dhcp_packet_rx.dhcp_subnet_mask}, Router: {dhcp_packet_rx.dhcp_router}" + f"DNS: {dhcp_packet_rx.dhcp_dns}, Domain: {dhcp_packet_rx.dhcp_domain_name}" ) socket.close() return ( IPv4Interface(str(dhcp_packet_rx.dhcp_yiaddr) + "/" + str(IPv4Address._make_netmask(str(dhcp_packet_rx.dhcp_subnet_mask))[1])), dhcp_packet_rx.dhcp_router[0], )