简体   繁体   English

每5秒ping远程计算机的批处理脚本?

[英]Batch script to ping remote computer every 5 seconds?

I'm trying to create a script that pings my remote computer like every 5 or 10 seconds to see if it is back online after rebooting. 我正在尝试创建一个脚本,每隔5或10秒对我的远程计算机进行ping操作,看看它是否在重新启动后重新联机。

I have this code that seems to work but spams like crazy, and I only need it to check every 5 seconds or so. 我有这个代码似乎工作,但垃圾邮件像疯了一样,我只需要每隔5秒检查一次。

A bonus to it would be if it stops the loop once it gets a successful connection. 如果它在成功连接后停止循环,那么它将获得奖励。

@echo off
:loop
echo Checking connection...

ping -n 1 xx.xxx.xxx.xx >nul
if errorlevel 1 (
  cls
  echo Computer is offline
  goto loop>nul

)

cls
echo Computer is online
  goto loop>nul
@echo off
:loop    
    ping -n 1 -w 500 -4 xx.xx.xx.xx 2>nul|find "TTL=" >nul||(echo offline & ping -w 1000 -n 6 localhost >nul & goto loop)
    echo online

Below would be the fine solution for your issue, i guess. 以下是我的问题的优秀解决方案。 try this script and let me know if tat works. 尝试这个脚本,让我知道如果tat工作。

#!/bin/ksh
pingSuccess=0
while [ 1 -eq 1 ]
do
        ping -c 1 $1 >/dev/null 2>&1 && pingSuccess=1
        [ ${pingSuccess} -eq 1 ] && echo "got the successful ping. " && break
        echo "ping failed. sleeping for 5 secs" && sleep 5
done
echo "exitting.."; exit 0

Did not test, but this should work: 没有测试,但这应该工作:

@echo off
SET IP=xxx.xxx.xxx.xxx
SET TIMEOUT=5000
:loop
echo Checking connection...

ping -n 1 %IP% >nul
if errorlevel not 0 (
  cls
  echo Computer at %IP% is offline, waiting %TIMEOUT%ms before retrying
  ping -n 1 1.0.0.1 -w %TIMEOUT% >nul
  goto loop>nul

)
echo Computer at %IP% is online

you can change 你可以改变

SET IP=xxx.xxx.xxx.xxx

to: 至:

SET IP=%1

and give the IP address as argument to the batch file. 并将IP地址作为批处理文件的参数。

Also, make sure IP 1.0.0.1 is not reachable, it is used to create the timeout. 此外,请确保无法访问IP 1.0.0.1,它用于创建超时。

Here is how I would do it. 我就是这样做的。

@echo off
setlocal

:pingagain
set /a inc+=5
set "ip=xx.xx.xx.xx"
Call :IsPingable %ip% && (
Echo %ip% is online
) || (
Echo %ip% is not online yet. 
ping  127.0.0.1 -n 1 -w 5000>nul
goto :pingagain)
exit /b   

:IsPingable <comp>
ping -n 1 -w 3000 -4 -l 8 "%~1" | Find "TTL=">nul  
exit /b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM