【问题标题】:Set timeout to a PHP function为 PHP 函数设置超时
【发布时间】:2013-08-07 13:13:25
【问题描述】:

我有这个脚本

<?php
function get_reverse_dns($Ip)
{
    $result = exec("nslookup -n ".escapeshellarg($Ip)." | grep 'name = '");
    if(strpos($result,"name =") === false)
    {
        return "NO REVERSE";
    }
    else
    {
        $result = trim($result);
        $ExplodedResult = explode("name =",$result);
        $ExplodedResult[1] = trim($ExplodedResult[1]);
        $ReverseDns = trim($ExplodedResult[1],".");
        return $ReverseDns;
    }
}
?>

这给了我反向 dns,现在的问题是,有时,一个 IP 可能会有很长的延迟,我希望这个脚本来检查它,IP 可以“查找”,如果 5 秒过去了,并且这没有发生,然后return false

我该怎么做?

我在linux下试过

nslookup --timeout 5 1.1.1.1 | grep 'name = '
timeout 5 nslookup 1.1.1.1 | grep 'name = '

谢谢。

【问题讨论】:

    标签: php linux bash function timeout


    【解决方案1】:

    我会使用挖掘: dig -x ${ip} +time=5 +tries=1 +retry=0 +short

    此命令将仅返回 IP 地址,因此将简化您的解析位。

    【讨论】:

    • 给我错误 dig -x ${"34.33.44.53"} +time=5 +tries=1 +retry=0 +short -bash: ${"34.33.44.53"}: 替换错误
    • ${} 是一种调用变量的方法。如果要直接输入IP地址,请去掉“${}”。 dig -x 34.33.44.53 +time=5 +tries=1 +retry=0 +short
    【解决方案2】:

    你想检查man nslookup,它会告诉你命令应该是:

    nslookup -timeout 5 1.1.1.1 | grep 'name = '
    

    你有一个太多的-的

    【讨论】:

    • 我看到了,我知道,但是如果我把任何 IP 放在那里,就会冻结并且不做任何事情......,甚至不是在 1 秒内查找的好 ips......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    • 1970-01-01
    • 2011-12-06
    • 2017-04-26
    相关资源
    最近更新 更多