How do I know what IPs my server have?

2020-04-18 09:14发布

问题:

I am using curl and I want to randomize the IP curl used with the IPs available for my server.

I need to know the IPs available for my server. Though I can hard code it, is there a PhP program for that?

回答1:

if you are in centos, run this command using php exec() and it will show you all availables ip interfaces.

ifconfig | grep inet | awk -F 'addr:' '{ print $2} ' | awk -F ' ' '{ print $1 }'


回答2:

If you need to know your server's external IP address, use this function:

function myIp()
{
    $url="simplesniff.com/ip";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}


标签: php ip