如何在批处理文件中检查 ping 是否响应

发布于 2024-09-05 16:23:34 字数 331 浏览 7 评论 0原文

我想连续 ping 服务器并在服务器响应时看到一个消息框,即服务器当前已关闭。我想通过批处理文件来完成它。

我可以显示一个消息框,如下所示 显示 Windows 批处理文件中的弹出窗口/消息框

,并且可以连续 ping

ping <servername> -t

但如何检查它是否响应?

I want to continuously ping a server and see a message box when ever it responds i.e. server is currently down. I want to do it through batch file.

I can show a message box as said here Show a popup/message box from a Windows batch file

and can ping continuously by

ping <servername> -t

But how do I check if it responded or not?

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

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

发布评论

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

评论(13

软糯酥胸 2024-09-12 16:23:35

问题是看看 ping 是否响应该脚本的响应。

但是,如果您收到“主机无法访问”消息,则此方法将不起作用,因为这将返回 ERRORLEVEL 0 并通过此脚本中使用的 Received = 1 检查,从脚本返回 Link is UP。当 ping 传送到目标网络但无法找到远程主机时,会发生主机无法访问的情况。

如果我记得检查 ping 是否成功的正确方法是使用 Find 查找字符串“TTL”。

@echo off
cls
set ip=%1
ping -n 1 %ip% | find "TTL"
if not errorlevel 1 set error=win
if errorlevel 1 set error=fail
cls
echo Result: %error%

这不适用于 IPv6 网络,因为 ping 在收到来自 IPv6 地址的回复时不会列出 TTL。

The question was to see if ping responded which this script does.

However this will not work if you get the Host Unreachable message as this returns ERRORLEVEL 0 and passes the check for Received = 1 used in this script, returning Link is UP from the script. Host Unreachable occurs when ping was delivered to target notwork but remote host cannot be found.

If I recall the correct way to check if ping was successful is to look for the string 'TTL' using Find.

@echo off
cls
set ip=%1
ping -n 1 %ip% | find "TTL"
if not errorlevel 1 set error=win
if errorlevel 1 set error=fail
cls
echo Result: %error%

This wont work with IPv6 networks because ping will not list TTL when receiving reply from IPv6 address.

春庭雪 2024-09-12 16:23:35

以下 checklink.cmd 程序是一个很好的起点。它依赖于这样一个事实:您可以执行单次 ping,并且如果成功,输出将包含以下行:

Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),

通过提取令牌 5 和 7 并检查它们分别是 "Received"“1,”,即可检测成功。

@setlocal enableextensions enabledelayedexpansion
@echo off
set ipaddr=%1
:loop
set state=down
for /f "tokens=5,6,7" %%a in ('ping -n 1 !ipaddr!') do (
    if "x%%b"=="xunreachable." goto :endloop
    if "x%%a"=="xReceived" if "x%%c"=="x1,"  set state=up
)
:endloop
echo.Link is !state!
ping -n 6 127.0.0.1 >nul: 2>nul:
goto :loop
endlocal

使用您要测试的名称(或 IP 地址)调用它:

checklink 127.0.0.1
checklink localhost
checklink nosuchaddress

请注意,如果您的区域设置不是英语,则必须将 Received 替换为您区域设置中的相应关键字,例如 recibidos 西班牙语。进行测试 ping 以发现您的区域设置中使用了哪个关键字。


要仅在状态发生变化时通知您,您可以使用:

@setlocal enableextensions enabledelayedexpansion
@echo off
set ipaddr=%1
set oldstate=neither
:loop
set state=down
for /f "tokens=5,7" %%a in ('ping -n 1 !ipaddr!') do (
    if "x%%a"=="xReceived" if "x%%b"=="x1," set state=up
)
if not !state!==!oldstate! (
    echo.Link is !state!
    set oldstate=!state!
)
ping -n 2 127.0.0.1 >nul: 2>nul:
goto :loop
endlocal

但是,正如 Gabe 在评论中指出的那样,您可以只使用 ERRORLEVEL ,因此相当于上面第二个脚本变成:

@setlocal enableextensions enabledelayedexpansion
@echo off
set ipaddr=%1
set oldstate=neither
:loop
set state=up
ping -n 1 !ipaddr! >nul: 2>nul:
if not !errorlevel!==0 set state=down
if not !state!==!oldstate! (
    echo.Link is !state!
    set oldstate=!state!
)
ping -n 2 127.0.0.1 >nul: 2>nul:
goto :loop
endlocal

The following checklink.cmd program is a good place to start. It relies on the fact that you can do a single-shot ping and that, if successful, the output will contain the line:

Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),

By extracting tokens 5 and 7 and checking they're respectively "Received" and "1,", you can detect the success.

@setlocal enableextensions enabledelayedexpansion
@echo off
set ipaddr=%1
:loop
set state=down
for /f "tokens=5,6,7" %%a in ('ping -n 1 !ipaddr!') do (
    if "x%%b"=="xunreachable." goto :endloop
    if "x%%a"=="xReceived" if "x%%c"=="x1,"  set state=up
)
:endloop
echo.Link is !state!
ping -n 6 127.0.0.1 >nul: 2>nul:
goto :loop
endlocal

Call it with the name (or IP address) you want to test:

checklink 127.0.0.1
checklink localhost
checklink nosuchaddress

Take into account that, if your locale is not English, you must replace Received with the corresponding keyword in your locale, for example recibidos for Spanish. Do a test ping to discover what keyword is used in your locale.


To only notify you when the state changes, you can use:

@setlocal enableextensions enabledelayedexpansion
@echo off
set ipaddr=%1
set oldstate=neither
:loop
set state=down
for /f "tokens=5,7" %%a in ('ping -n 1 !ipaddr!') do (
    if "x%%a"=="xReceived" if "x%%b"=="x1," set state=up
)
if not !state!==!oldstate! (
    echo.Link is !state!
    set oldstate=!state!
)
ping -n 2 127.0.0.1 >nul: 2>nul:
goto :loop
endlocal

However, as Gabe points out in a comment, you can just use ERRORLEVEL so the equivalent of that second script above becomes:

@setlocal enableextensions enabledelayedexpansion
@echo off
set ipaddr=%1
set oldstate=neither
:loop
set state=up
ping -n 1 !ipaddr! >nul: 2>nul:
if not !errorlevel!==0 set state=down
if not !state!==!oldstate! (
    echo.Link is !state!
    set oldstate=!state!
)
ping -n 2 127.0.0.1 >nul: 2>nul:
goto :loop
endlocal
新雨望断虹 2024-09-12 16:23:35

我知道这是一个旧线程,但我想测试我的系统上是否有一台机器已启动,除非我误解了,否则如果我的路由器报告地址无法访问,则上述所有操作都不起作用。我使用的是批处理文件而不是脚本,因为我想在几乎任何 WIN 机器上“KISS”。因此,我使用的方法是执行多个 ping 并测试“Lost = 0”,如下所示,

ping -n 2 %pingAddr% | find /I "Lost = 0"  
if %errorlevel% == 0 goto OK

我还没有对此进行严格测试,但到目前为止它对我来说已经完成了这项工作

I know this is an old thread, but I wanted to test if a machine was up on my system and unless I have misunderstood, none of the above works if my router reports that an address is unreachable. I am using a batch file rather than a script because I wanted to "KISS" on pretty much any WIN machine. So the approach I used was to do more than one ping and test for "Lost = 0" as follows

ping -n 2 %pingAddr% | find /I "Lost = 0"  
if %errorlevel% == 0 goto OK

I haven't tested this rigorously but so far it does the job for me

﹎☆浅夏丿初晴 2024-09-12 16:23:35

我根据 paxdiablo 的帖子制定了一个变体解决方案

将以下代码放入 Waitlink.cmd

@setlocal enableextensions enabledelayedexpansion
@echo off
set ipaddr=%1
:loop
set state=up
ping -n 1 !ipaddr! >nul: 2>nul:
if not !errorlevel!==0 set state=down
echo.Link is !state!
if "!state!"=="up" (
  goto :endloop
)
ping -n 6 127.0.0.1 >nul: 2>nul:
goto :loop
:endloop
endlocal

例如,从另一个批处理文件中使用它,如下所示

call Waitlink someurl.com
net use o: \\someurl.com\myshare

对 waitlink 的调用仅在 ping 成功时才会返回。感谢帕克斯迪亚布罗和加布。希望这对其他人有帮助。

I have made a variant solution based on paxdiablo's post

Place the following code in Waitlink.cmd

@setlocal enableextensions enabledelayedexpansion
@echo off
set ipaddr=%1
:loop
set state=up
ping -n 1 !ipaddr! >nul: 2>nul:
if not !errorlevel!==0 set state=down
echo.Link is !state!
if "!state!"=="up" (
  goto :endloop
)
ping -n 6 127.0.0.1 >nul: 2>nul:
goto :loop
:endloop
endlocal

For example use it from another batch file like this

call Waitlink someurl.com
net use o: \\someurl.com\myshare

The call to waitlink will only return when a ping was succesful. Thanks to paxdiablo and Gabe. Hope this helps someone else.

—━☆沉默づ 2024-09-12 16:23:35

以下是我发现的内容:

:pingtheserver
ping %input% | find "Reply" > nul
if not errorlevel 1 (
    echo server is online, up and running.
) else (
    echo host has been taken down wait 3 seconds to refresh
    ping 1.1.1.1 -n 1 -w 3000 >NUL
    goto :pingtheserver
) 

请注意,ping 1.1.1.1 -n -w 1000 >NUL 将等待 1 秒,但仅在连接到网络时有效

Here's something I found:

:pingtheserver
ping %input% | find "Reply" > nul
if not errorlevel 1 (
    echo server is online, up and running.
) else (
    echo host has been taken down wait 3 seconds to refresh
    ping 1.1.1.1 -n 1 -w 3000 >NUL
    goto :pingtheserver
) 

Note that ping 1.1.1.1 -n -w 1000 >NUL will wait 1 second but only works when connected to a network

命硬 2024-09-12 16:23:35

感谢@paxdiablo 和@Jan Lauridsen
这是我对检查和 IP(本地计算机)的修改,适用于从 dhcp 服务器或任何其他问题断开连接的情况,在 WinXP PRO SP3

checkconnection.cmd 下测试:

@setlocal enableextensions enabledelayedexpansion
@echo off
set ipaddr=8.8.8.8
:loop

for /f "tokens=5,6,7" %%a in ('ping -n 1 !ipaddr!') do (
    if "x%%b"=="xunreachable." goto :endloop
    if "x%%b"=="xtimed out." goto :endloop
    if "x%%a"=="xReceived" if "x%%c"=="x1,"  goto :up  
)

:endloop
set state=Down
echo.Connection is !state!
ping -n 2 127.0.0.1 >nul: 2>nul: 
echo starting Repairing at %date% %time%>>D:\connection.log
call repair.cmd>>D:\connection.log
ping -n 10 127.0.0.1 >nul: 2>nul:
goto :loop
endlocal
:up
set state=Up
echo.Connection is !state!
ping -n 6 127.0.0.1 >nul: 2>nul:
cls
goto :loop
endlocal

如果没有 ping 响应谷歌 DNS 然后开始修复,我为此目的设置了静态 IP,但它也应该适用于动态。

repair.cmd

route -f
ipconfig /release
ipconfig /renew
arp -d *
nbtstat -R
nbtstat -RR
ipconfig /flushdns
ipconfig /registerdns
cls

我最诚挚的问候

thanks to @paxdiablo and @Jan Lauridsen
this is my modification to check and IP (local machine), good for case which connection is dropped either from dhcp server or any other issue, tested Under WinXP PRO SP3

checkconnection.cmd:

@setlocal enableextensions enabledelayedexpansion
@echo off
set ipaddr=8.8.8.8
:loop

for /f "tokens=5,6,7" %%a in ('ping -n 1 !ipaddr!') do (
    if "x%%b"=="xunreachable." goto :endloop
    if "x%%b"=="xtimed out." goto :endloop
    if "x%%a"=="xReceived" if "x%%c"=="x1,"  goto :up  
)

:endloop
set state=Down
echo.Connection is !state!
ping -n 2 127.0.0.1 >nul: 2>nul: 
echo starting Repairing at %date% %time%>>D:\connection.log
call repair.cmd>>D:\connection.log
ping -n 10 127.0.0.1 >nul: 2>nul:
goto :loop
endlocal
:up
set state=Up
echo.Connection is !state!
ping -n 6 127.0.0.1 >nul: 2>nul:
cls
goto :loop
endlocal

if no ping response from google DNS then start repair, i had static IP set for this purpose but it should work with dinamic as well.

repair.cmd:

route -f
ipconfig /release
ipconfig /renew
arp -d *
nbtstat -R
nbtstat -RR
ipconfig /flushdns
ipconfig /registerdns
cls

My Best Regards

青芜 2024-09-12 16:23:35

您可以不使用“-t”进行 ping 操作并检查 ping 的退出代码。当没有答案时,它会报告失败。

You can ping without "-t" and check the exit code of the ping. It reports failure when there is no answer.

熊抱啵儿 2024-09-12 16:23:35

简单版本:

for /F "delims==, tokens=4" %a IN ('ping -n 2 127.0.0.1 ^| findstr /R "^Packets: Sent =.$"') DO (

if %a EQU 2 (
echo Success
) ELSE (
echo FAIL
)

)

但有时第一个 ping 失败,第二个 ping 有效(反之亦然),对吗?因此,我们希望在至少成功返回一个 ICMP 回复时获得成功:

for /F "delims==, tokens=4" %a IN ('ping -n 2 192.168.1.1 ^| findstr /R "^Packets: Sent =.$"') DO (

if %a EQU 2 (
echo Success
) ELSE (
if %a EQU 1 (
echo Success
) ELSE (
echo FAIL
)
)

)

Simple version:

for /F "delims==, tokens=4" %a IN ('ping -n 2 127.0.0.1 ^| findstr /R "^Packets: Sent =.$"') DO (

if %a EQU 2 (
echo Success
) ELSE (
echo FAIL
)

)

But sometimes first ping just fail and second one work (or vice versa) right? So we want to get success when at least one ICMP reply has been returned successfully:

for /F "delims==, tokens=4" %a IN ('ping -n 2 192.168.1.1 ^| findstr /R "^Packets: Sent =.$"') DO (

if %a EQU 2 (
echo Success
) ELSE (
if %a EQU 1 (
echo Success
) ELSE (
echo FAIL
)
)

)
忆梦 2024-09-12 16:23:35

我希望这对某人有帮助。在检查各个路径之前,我使用这一点逻辑来验证网络共享是否响应。它应该处理 DNS 名称和 IP 地址

文本文件中的有效路径是
\192.168.1.2\'文件夹' 或 \NAS\'文件夹'

@echo off
title Network Folder Check

pushd "%~dp0"
:00
cls

for /f "delims=\\" %%A in (Files-to-Check.txt) do set Server=%%A
    setlocal EnableDelayedExpansion
        ping -n 1 %Server% | findstr TTL= >nul 
            if %errorlevel%==1 ( 
                ping -n 1 %Server% | findstr "Reply from" | findstr "time" >nul
                    if !errorlevel!==1 (echo Network Asset %Server% Not Found & pause & goto EOF)
            )
:EOF

I hope this helps someone. I use this bit of logic to verify if network shares are responsive before checking the individual paths. It should handle DNS names and IP addresses

A valid path in the text file would be
\192.168.1.2\'folder' or \NAS\'folder'

@echo off
title Network Folder Check

pushd "%~dp0"
:00
cls

for /f "delims=\\" %%A in (Files-to-Check.txt) do set Server=%%A
    setlocal EnableDelayedExpansion
        ping -n 1 %Server% | findstr TTL= >nul 
            if %errorlevel%==1 ( 
                ping -n 1 %Server% | findstr "Reply from" | findstr "time" >nul
                    if !errorlevel!==1 (echo Network Asset %Server% Not Found & pause & goto EOF)
            )
:EOF
你没皮卡萌 2024-09-12 16:23:35

我稍微修改了 PaxDiablo 的代码,以更好地适应我正在做的事情,并认为我会分享。我的目标是循环遍历 IP 地址列表,看看是否有剩余的 IP 地址,并且此代码将在周末最后一个班次结束时运行,以检查每个人是否都按照之前关闭所有 PC 的说明进行操作。他们回家了。

由于在 for 循环中使用 goto 会中断所有 for 循环,而不仅仅是最低的嵌套 for 循环,因此 PaxDiablo 的代码在到达无法访问的 IP 地址时会停止处理其他 IP 地址。我发现我可以添加第二个变量来跟踪它是无法访问的,而不是退出循环,现在这段代码现在可以完美地为我运行。

我将文件另存为 CheckPCs.bat,虽然我猜测这不是正确的编码实践,但我确实在代码下方列出了 IP 地址以及说明(在我的情况下是 PC 的物理位置)。正如其他人所提到的,您将必须进一步修改代码以适应其他本地化和 IPV6。

@setlocal enableextensions enabledelayedexpansion
@echo off
for /f "tokens=2,3 delims=/ skip=16" %%i in (CheckPCs.bat) do (
set ipaddr=%%i
set state=shut down
set skip=0
for /f "tokens=5,6,7" %%a in ('ping -n 1 !ipaddr!') do (
    if "x%%b"=="xunreachable." set skip=1
    if !skip!==0 if "x%%a"=="xReceived" if "x%%c"=="x1,"  set state=still on
)
echo %%i %%j is !state!

)
pause
endlocal

rem /IP ADDRESS0/COMPUTER NAME0
rem /IP ADDRESS1/COMPUTER NAME1

如果您确实需要修改此代码,请注意以下几点:

for /f "tokens=2,3 delims=/ skip=16" %%i in (CheckPCs.bat) do (

for 循环一次处理一行 CheckPCs.bat。 Skip 告诉它忽略前 16 行并直接转到 IP 地址。我使用 / 作为分隔符没有任何特殊原因,但请注意,如果您要 ping Web 地址而不是 IP,则必须更改分隔符。类似管道字符 |会起作用的。当然,由于该行已用 rem 注释掉,因此 rem 成为第一个令牌,这意味着我只想使用令牌 2 和 3,它们将是 IP 地址和 PC 描述。后一个字段是可选的,您只需修改代码即可将其删除。

您可能应该修改用于状态的术语,而 echo %%i %%j is !state!以便您的术语清晰简洁。如果你想在某个地方记录状态,你可以通过附加>>将其输入到文本文件中。 file.txt 到该行。在这种情况下,您可能还想添加日期/时间。

最后,经过适当编码培训的人可能知道,带有标记的批处理 for 循环的工作方式(简单来说)是文本的每个部分在每个分隔符处分开,默认为空格,然后对其进行分配到一个 %% 变量,其名称以您指定的字符开始,然后在 ascii 字符列表中递增。这意味着如果我指定从 %%i 开始,下一个标记将是 %%j,然后是 %%k,依此类推。如果我使用 %%B,接下来将是 %%C,然后是 %%D 等等。我在该主题上阅读的每个线程最多可以有 31 个标记。

I've modified PaxDiablo's code slightly to better fit with what I was doing and thought I'd share. My objective was to loop through a list of IP addresses to see if any were left on, and this code is to be run at the end of the last shift of the weekend to check if everyone is following the instructions to shut down all PCs before they go home.

Since using a goto in a for loop breaks out of all for loops not just the lowest nested for loop, PaxDiablo's code stopped processing further IP addresses when it got to one that was unreachable. I found that I could add a second variable to track that it was unreachable rather than exiting the loop and now this code is now running perfectly for me.

I have the file saved as CheckPCs.bat and though I'm guessing it's not proper coding practice, I do have the IP addresses listed below the code along with a description which in my case is the physical location of the PC. As mentioned by others you will have to modify the code further for other localizations and for IPV6.

@setlocal enableextensions enabledelayedexpansion
@echo off
for /f "tokens=2,3 delims=/ skip=16" %%i in (CheckPCs.bat) do (
set ipaddr=%%i
set state=shut down
set skip=0
for /f "tokens=5,6,7" %%a in ('ping -n 1 !ipaddr!') do (
    if "x%%b"=="xunreachable." set skip=1
    if !skip!==0 if "x%%a"=="xReceived" if "x%%c"=="x1,"  set state=still on
)
echo %%i %%j is !state!

)
pause
endlocal

rem /IP ADDRESS0/COMPUTER NAME0
rem /IP ADDRESS1/COMPUTER NAME1

Some notes if you do need to modify this code:

for /f "tokens=2,3 delims=/ skip=16" %%i in (CheckPCs.bat) do (

The for loop is processing CheckPCs.bat one line at a time. Skip is telling it to ignore the first 16 lines and go straight to the IP addresses. I'm using / as a delimiter for no particular reason but note that if you are pinging web addresses instead of IP, you'll have to change the delimiter. Something like the pipe character | would work. Of course since the line is commented out with rem, rem becomes the first token which means I only want to work with tokens 2 and 3 which will be the IP address and PC description. The latter field is optional, you'd just have to modify the code to remove it.

You should probably modify the terminology used for state and for echo %%i %%j is !state! so that the terminology is clear and concise for you. If you want to record the state somewhere, you can just feed it into a text file by appending >> file.txt to the line. You might want to also add a date/time in that case.

Lastly, something people with proper training in coding these might know, the way a batch for loop with tokens works (in simple terms) is each section of the text is split up at each delimiter, the default being space, and then it is assigned to a %% variable whose name begins at whichever character you specify and then increases up the ascii character list. This means if I specify to start at %%i, the next token will be %%j, then %%k and so on. If I used %%B, next would be %%C, then %%D etc. There can be a maximum of 31 tokens per another thread I read on the topic.

柠檬 2024-09-12 16:23:35

以下@Dan W回答:

@echo off

set Server=192.168.0.18
setlocal EnableDelayedExpansion
:checkhost
ping -n 1 %Server% | findstr TTL= >nul 
if %errorlevel%==1 ( 
    ping -n 1 %Server% | findstr "Reply from" | findstr "time" >nul
    if !errorlevel!==1 (echo Network Asset %Server% Not Found & goto checkhost)
)

Following @Dan W answer:

@echo off

set Server=192.168.0.18
setlocal EnableDelayedExpansion
:checkhost
ping -n 1 %Server% | findstr TTL= >nul 
if %errorlevel%==1 ( 
    ping -n 1 %Server% | findstr "Reply from" | findstr "time" >nul
    if !errorlevel!==1 (echo Network Asset %Server% Not Found & goto checkhost)
)
千と千尋 2024-09-12 16:23:35

我看到了 ping 的三个结果 - 我们“想要”IP 回复的结果、“主机无法访问”和“超时”(不确定确切的措辞)。

前两个返回 ERRORLEVEL 为 0。

超时返回 ERRORLEVEL 为 1。

是否可能返回其他结果和错误级别? (除了使用返回允许的开关和错误级别 1 的无效开关之外。)

显然 Host Unreachable 可以使用之前发布的方法之一(尽管很难弄清楚何时有人回复他们正在为哪种情况编写代码),但确实超时以类似的方式返回,可以解析?

一般来说,如何知道 ping 结果的哪一部分可以被解析? (即,为什么发送和/或接收和/或 TTL 可以解析,但主机无法访问?

哦,iSid,也许没有太多赞成票,因为阅读本文的人没有足够的积分。所以他们得到他们的问题得到了回答(或没有回答),然后

我没有将上面的内容作为答案发布,但我没有看到这个选择。

I've seen three results to a ping - The one we "want" where the IP replies, "Host Unreachable" and "timed out" (not sure of exact wording).

The first two return ERRORLEVEL of 0.

Timeout returns ERRORLEVEL of 1.

Are the other results and error levels that might be returned? (Besides using an invalid switch which returns the allowable switches and an errorlevel of 1.)

Apparently Host Unreachable can use one of the previously posted methods (although it's hard to figure out when someone replies which case they're writing code for) but does the timeout get returned in a similar manner that it can be parsed?

In general, how does one know what part of the results of the ping can be parsed? (Ie, why might Sent and/or Received and/or TTL be parseable, but not host unreachable?

Oh, and iSid, maybe there aren't many upvotes because the people that read this don't have enough points. So they get their question answered (or not) and leave.

I wasn't posting the above as an answer. It should have been a comment but I didn't see that choice.

无人问我粥可暖 2024-09-12 16:23:35
#!/bin/bash
logPath="pinglog.txt"

while(true)
      do
          # refresh the timestamp before each ping attempt
          theTime=$(date -Iseconds)

          # refresh the ping variable
          ping google.com -n 1

            if [ $? -eq 0 ] 
            then
                 echo $theTime + '| connection is up' >> $logPath
            else
                 echo $theTime + '| connection is down' >> $logPath
          fi
            Sleep 1
             echo ' '
      done
#!/bin/bash
logPath="pinglog.txt"

while(true)
      do
          # refresh the timestamp before each ping attempt
          theTime=$(date -Iseconds)

          # refresh the ping variable
          ping google.com -n 1

            if [ $? -eq 0 ] 
            then
                 echo $theTime + '| connection is up' >> $logPath
            else
                 echo $theTime + '| connection is down' >> $logPath
          fi
            Sleep 1
             echo ' '
      done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文