ping 客户端的最佳方法

发布于 2024-11-07 15:45:30 字数 297 浏览 0 评论 0原文

是的,我有一个 PHP 脚本正在工作,其中服务器 ping 是客户端。我面临的问题是,有时服务器无法联系客户端,尽管当我手动 ping 客户端时,它可以成功 ping 通。

我使用的 ping 命令是 ping -q -w 3 -c 1 ipaddresshere >

ping 客户端的最佳方式是什么,如果 ping 在某个时间之前失败,可能会2/3 次,留下2/3 秒的间隙重试?

Right, I have a PHP script at work where the server ping's a client. The problem I am facing is that sometimes the server cannot contact the client although when I manually ping the client it ping's successfully.

The ping command I am using is this ping -q -w 3 -c 1 < ipaddresshere >

What would be the best way of pinging the clients maybe 2/3 times leaving like a 2/3 second gap if a ping fails before a retry?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

关于从前 2024-11-14 15:45:30

当您在 Unix 环境中时,您始终可以创建然后调用 shell 脚本来处理循环和等待。但令我惊讶的是你不能在 php 内部做到这一点。

另外,我不确定你的示例 ping 命令,我检查的 2 个不同环境似乎对你提到的选项有不同的含义,而不是你想要的。尝试 man pingping --help

下面的脚本应该为您提供一个用于实现 ping 重试的框架,但我不能在上面花费大量时间。

cat pingCheck.sh

#! /bin/bash -vx

IPaddr=$1

: ${maxPingTries:=3}
echo "maxPingTries=${maxPingTries}"

pingTries=0
while ${keepTryingToPing:-true} ; do
  if ping -n 3 -r 1 ${IPaddr} ;then
    keepTryingToPing=false
  else
    sleep ${sleepSecs:-3}
    if (( ++pingTries >= maxPingTries )) ; then
      printf "Execeeded count on ping attempts = ${maxPingTries}\n" 1>&2
      keepTryingToPing=false
    fi
  fi

done

我希望这有帮助。

PS,由于您似乎是新用户,如果您得到的答案对您有帮助,请记住将其标记为已接受,和/或给它 +(或 -)作为有用的答案。

As you are in the unix environment, you can always make and then call a shell script to handle the looping and waiting. But I'm surprised that you can't do that inside of php.

Also, i'm not sure about your sample ping command, the 2 different environments I checked seem to have different meanings for the options you mention than what you seem to intend. Try man ping OR ping --help

The script below should give you a framework for implementing a ping-retry, but I can't spend a lot of time on it.

cat pingCheck.sh

#! /bin/bash -vx

IPaddr=$1

: ${maxPingTries:=3}
echo "maxPingTries=${maxPingTries}"

pingTries=0
while ${keepTryingToPing:-true} ; do
  if ping -n 3 -r 1 ${IPaddr} ;then
    keepTryingToPing=false
  else
    sleep ${sleepSecs:-3}
    if (( ++pingTries >= maxPingTries )) ; then
      printf "Execeeded count on ping attempts = ${maxPingTries}\n" 1>&2
      keepTryingToPing=false
    fi
  fi

done

I hope this helps.

P.S. as you appear to be a new user, if you get an answer that helps you please remember to mark it as accepted, and/or give it a + (or -) as a useful answer.

煞人兵器 2024-11-14 15:45:30

对于 php,您可以尝试 PEAR 的 Net_PING 包。

这是一个指导您完成此操作的链接
http://www.codediesel.com/php/ping-a-服务器使用-php/

for php, you can try PEAR's Net_PING package.

here is a link guiding you through it
http://www.codediesel.com/php/ping-a-server-using-php/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文