2008服务器上的批处理脚本 - 如果Ping成功则

时间:2014-12-17 13:17:47

标签: batch-file command-line windows-server-2008

我正在尝试编写一个脚本,在运行我的其余脚本之前检查计算机是否在我的LAN上。这是一个使用 robocopy 的简单备份脚本,但我希望它根据文件是否成功输出文件。 这就是我所拥有的。

set machine=userbox
ping -n 1 %machine% > nul
if errorlevel 1 goto BAD
goto GOOD
:GOOD
robocopy source destination
echo "backup complete" > c:\scripts\backupgood-%machine%.log
shutdown /s /m \\%machine% /t 600
goto END
:BAD
echo "Computer not on" > c:\scripts\%machine%-offline.log
:END

现在,脚本没有检测到系统是否打开,并且假设它已打开并继续使用脚本,就好像机器能够被ping了一样。

有人可以指出错误未通过的原因,或者某人有更好的方法来确定系统是否在线。

提前致谢。

3 个答案:

答案 0 :(得分:4)

"问题"使用您的代码管理errorlevel以确定机器是否在线。

问题是:ping表现如何?,errorlevel何时设置?

如果我们使用ipv6,规则是

  • errorlevel在没有回复所有已发送的数据包(所有数据包丢失)时设置

  • 如果对任何已发送的数据包有回复,则
  • errorlevel未设置

ipv6具有一致的行为,检查errorlevel是了解机器是否在线的可靠方法。

在ipv4中,规则是不同的

    当没有回复至少一个已发送的数据包时,
  • errorlevel被设置

  • 当回复所有已发送的数据包(没有数据包丢失)时,
  • errorlevel未设置

但ping同一子网上的一台不可用的机器并没有设置errorlevel,你得到一个"无法访问"使用n packets sent, n packed received, 0 packets lost回答,所有数据包都会从发送数据包的同一台机器获得回复。

当机器在同一子网中时,ipv4中的此行为会导致错误级别检查失败。

如何解决ipv4中的问题?

可以检查ping命令的输出,如果输出中存在字符串TTL=,则目标计算机处于联机状态。

ping -n 1 10.0.0.1 | find "TTL=" >nul 
if errorlevel 1 ( 
    echo offline 
) else (
    echo online
)

对于"将军"解决方案,这个(改编自先前的答案)可以使用(似乎很多代码,但几乎都是评论)

@echo off

    setlocal enableextensions disabledelayedexpansion

    if "%~1"=="" goto :eof

    call :isOnline "%~1"
    if not errorlevel 1 ( echo ONLINE ) else ( echo OFFLINE )

    endlocal
    exit /b

:isOnline address pingCount
    setlocal enableextensions disabledelayedexpansion

    :: send only one ping packed unless it is indicated to send more than one
    set /a "pingCount=0", "pingCount+=%~2" >nul 2>nul 
    if %pingCount% lss 1 set "pingCount=1"

    :: a temporary file is needed to capture ping output for later processing
    set "tempFile=%temp%\%~nx0.%random%.tmp"

    :: ping the indicated address getting command output and errorlevel
    ping -w 1000 -n %pingCount% "%~1" > "%tempFile%"  && set "pingError=" || set "pingError=1"

    ::
    :: When pinging, the behaviours of ipv4 and ipv6 are different
    ::
    :: we get errorlevel = 1 when
    ::    ipv4 - when at least one packet is lost. When sending more than one packet
    ::           the easiest way to check for reply is search the string "TTL=" in 
    ::           the output of the command.
    ::    ipv6 - when all packet are lost.
    ::
    :: we get errorlevel = 0 when
    ::    ipv4 - all packets are received. BUT pinging a inactive host on the same  
    ::           subnet result in no packet lost. It is necessary to check for "TTL=" 
    ::           string in the output of the ping command
    ::    ipv6 - at least one packet reaches the host
    ::
    :: We can try to determine if the input address (or host name) will result in 
    :: ipv4 or ipv6 pinging, but it is easier to check the result of the command
    ::
    ::                          +--------------+-------------+
    ::                          | TTL= present |    No TTL   | 
    ::  +-----------------------+--------------+-------------+
    ::  | ipv4    errorlevel 0  |      OK      |    ERROR    |
    ::  |         errorlevel 1  |      OK      |    ERROR    | 
    ::  +-----------------------+--------------+-------------+ 
    ::  | ipv6    errorlevel 0  |              |      OK     |
    ::  |         errorlevel 1  |              |    ERROR    |
    ::  +-----------------------+----------------------------+
    ::
    :: So, if TTL= is present in output, host is online. If TTL= is not present,  
    :: errorlevel is 0 and the address is ipv6 then host is online. In the rest 
    :: of the cases host is offline.
    ::
    :: To determine the ip version, a regular expresion to match a ipv6 address is 
    :: used with findstr. As it will be only tested in the case of no errorlevel, 
    :: the ip address will be present in ping command output.

    set "exitCode=1"
    >nul 2>nul (
        find "TTL=" "%tempFile%" && ( set "exitCode=0" ) || (
            if not defined pingError (
                findstr /r /c:" [a-f0-9:][a-f0-9]*:[a-f0-9:%%]*[a-f0-9]: " "%tempFile%" && set "exitCode=0"
            )
        )
        del /q "%tempFile%"
    )

    :: cleanup and return errorlevel: 0=online , 1=offline 
    endlocal & exit /b %exitCode%

答案 1 :(得分:1)

您可以检查是否存在远程目录:

IF EXIST "\\%machine%\Share\To\Check" (
    ECHO %machine% is available.
) ELSE (
    ECHO %machine% is not available.
)

答案 2 :(得分:0)

多谢你们俩。我试过了两个,但最终这对我有用了

set machine=userbox
ping -n 1 %machine% | find "TTL=" >nul
IF errorlevel 1 (
echo "Computer Not On" %date% %time% > c:\scripts\%machine%-computeroff.log
) ELSE (
robocopy source destination
shutdown.exe /s /m \\%machine% /c "Mandatory Shutdown in progress. You have 10 minutes" /t 600
echo "shutdown command given" %date% %time% > c:\scripts\shutdown-%machine%.log 

干杯

相关问题