drv13270 2011-05-16 11:40
浏览 34
已采纳

ping客户端的最佳方式

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?

  • 写回答

2条回答 默认 最新

  • donglin5770 2011-05-18 18:48
    关注

    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}
    " 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题