def set_dhcp(self, hostname=None, response_timeout=30):
        """Initializes the DHCP client and attempts to retrieve
        and set network configuration from the DHCP server.
        Returns 0 if DHCP configured, -1 otherwise.

        :param str hostname: The desired hostname, with optional {} to fill in MAC.
        :param int response_timeout: Time to wait for server to return packet, in seconds.
        """
        if self._debug:
            print("* Initializing DHCP")

        # Return IP assigned by DHCP
        self._dhcp_client = dhcp.DHCP(self,
                                      self.mac_address,
                                      hostname,
                                      response_timeout,
                                      debug=self._debug)
        ret = self._dhcp_client.request_dhcp_lease()
        if ret == 1:
            if self._debug:
                _ifconfig = self.ifconfig
                print("* Found DHCP Server:")
                print("IP: {}\nSubnet Mask: {}\nGW Addr: {}\nDNS Server: {}".
                      format(*_ifconfig))
            return 0
        return -1
Пример #2
0
    def set_dhcp(self, hostname=None, response_timeout=3):
        """Initializes the DHCP client and attempts to retrieve
        and set network configuration from the DHCP server.
        Returns True if DHCP configured, False otherwise.
        :param str hostname: The desired hostname, with optional {} to fill in MAC.
        :param int response_timeout: Time to wait for server to return packet, in seconds.

        """
        if self._debug:
            print("* Initializing DHCP")
        self._src_port = 68
        # Return IP assigned by DHCP
        _dhcp_client = dhcp.DHCP(
            self, self.mac_address, hostname, response_timeout, debug=self._debug
        )
        ret = _dhcp_client.request_dhcp_lease()
        if ret == 1:
            _ip = (
                _dhcp_client.local_ip[0],
                _dhcp_client.local_ip[1],
                _dhcp_client.local_ip[2],
                _dhcp_client.local_ip[3],
            )

            _subnet_mask = (
                _dhcp_client.subnet_mask[0],
                _dhcp_client.subnet_mask[1],
                _dhcp_client.subnet_mask[2],
                _dhcp_client.subnet_mask[3],
            )

            _gw_addr = (
                _dhcp_client.gateway_ip[0],
                _dhcp_client.gateway_ip[1],
                _dhcp_client.gateway_ip[2],
                _dhcp_client.gateway_ip[3],
            )

            self._dns = (
                _dhcp_client.dns_server_ip[0],
                _dhcp_client.dns_server_ip[1],
                _dhcp_client.dns_server_ip[2],
                _dhcp_client.dns_server_ip[3],
            )
            self.ifconfig = (_ip, _subnet_mask, _gw_addr, self._dns)
            if self._debug:
                print("* Found DHCP Server:")
                print(
                    "IP: {}\nSubnet Mask: {}\nGW Addr: {}\nDNS Server: {}".format(
                        _ip, _subnet_mask, _gw_addr, self._dns
                    )
                )
            self._src_port = 0
            return 0
        return -1
    def set_dhcp(self, hostname=None, response_timeout=30):
        """Initializes the DHCP client and attempts to retrieve
        and set network configuration from the DHCP server.
        Returns 0 if DHCP configured, -1 otherwise.
        :param str hostname: The desired hostname, with optional {} to fill in MAC.
        :param int response_timeout: Time to wait for server to return packet, in seconds.

        """
        if self._debug:
            print("* Initializing DHCP")

        # First, wait link status is on
        # to avoid the code during DHCP - assert self.link_status, "Ethernet cable disconnected!"
        start_time = time.monotonic()
        while True:
            if self.link_status or ((time.monotonic() - start_time) > 5):
                break
            time.sleep(1)
            if self._debug:
                print("My Link is:", self.link_status)

        # Return IP assigned by DHCP
        self._dhcp_client = dhcp.DHCP(
            self, self.mac_address, hostname, response_timeout, debug=self._debug
        )
        ret = self._dhcp_client.request_dhcp_lease()
        if ret == 1:
            if self._debug:
                _ifconfig = self.ifconfig
                print("* Found DHCP Server:")
                print(
                    "IP: {}\nSubnet Mask: {}\nGW Addr: {}\nDNS Server: {}".format(
                        *_ifconfig
                    )
                )
            return 0
        return -1