DataSift\Storyplayer\OsLib\Base_Centos7::determineIpAddress PHP Method

determineIpAddress() public method

public determineIpAddress ( HostDetails $hostDetails, DataSift\Storyplayer\HostLib\SupportedHost $host ) : string
$hostDetails HostDetails
$host DataSift\Storyplayer\HostLib\SupportedHost
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);
    }