public function determineHostname($hostDetails, SupportedHost $host) { // what are we doing? $log = usingLog()->startAction("query " . basename(__CLASS__) . " for hostname"); // how do we do this? $command = "hostname"; $result = $host->runCommandViaHostManager($hostDetails, $command); if ($result->didCommandSucceed()) { $lines = explode("\n", $result->output); $hostname = trim($lines[0]); $hostname = $this->runHostnameSafeguards($hostDetails, $hostname); $log->endAction("hostname is '{$hostname}'"); return $hostname; } // if we get here, we do not know what the hostname is $msg = "could not determine hostname"; $log->endAction($msg); throw new E5xx_ActionFailed(__METHOD__, $msg); }
/** * * @param HostDetails $hostDetails * @param SupportedHost $host * @return string */ public function determineIpAddress($hostDetails, SupportedHost $host) { // what are we doing? $log = usingLog()->startAction("query " . basename(__CLASS__) . " for IP address"); if (empty($hostDetails->ifaces)) { // set default network interfaces $hostDetails->ifaces = array('eth1', 'eth0'); } // how do we do this? foreach ($hostDetails->ifaces as $iface) { $command = "/sbin/ip addr show {$iface}"; $result = $host->runCommandViaHostManager($hostDetails, $command); // NOTE: the above command will return the exit code 0 even if the interface is not found if ($result->didCommandFail() || strpos($result->output, 'error fetching') !== false) { // no interface found // // move on to the next interface to check continue; } // reduce the output down to an IP address $lines = explode("\n", $result->output); $lines = FilterForMatchingString::against($lines, 'inet '); $lines = FilterColumns::from($lines, '1', ' '); // do we have an IP address? if (!isset($lines[0]) || empty(trim($lines[0]))) { // no, we do not continue; } // if we get here, then we have an IP address/netmask combo $parts = explode("/", $lines[0]); $ipAddress = trim($parts[0]); $log->endAction("IP address is '{$ipAddress}'"); return $ipAddress; } // if we get here, we do not know what the IP address is $msg = "could not determine IP address"; $log->endAction($msg); throw new E5xx_ActionFailed(__METHOD__, $msg); }