【问题标题】:Check the machine is accessible or not using command检查机器是否可访问或不使用命令
【发布时间】:2013-07-30 14:50:52
【问题描述】:

我有一个批处理文件,我正在其中重新启动机器并想检查机器是否重新联机,然后在特定时间后执行代码块,否则通知用户机器不在线 我想检查以下内容:

pingresult = ping \\machinename

if (pingresult == true)
{
  execute some task
}
else
{
   keep  pinging for say 5min. if after 5mins machine is not up show message to user
}

【问题讨论】:

    标签: command-line batch-file command


    【解决方案1】:

    这将尝试 ping %machinename% 100 次,默认超时时间为 3 秒:3s * 100 = 300s,即 5 分钟。

    for /L %%N IN (1, 1, 100) DO (
        ping -n 1 %machinename%
        if not ERRORLEVEL 1 (
            set pingresult=true
            goto done
        )
    )
    set pingresult=false
    :done
    if %pingresult% == true (
      echo "ping ok, doing something..."
    ) else (
      echo "no reply after 5 mins, error!"
    )
    

    【讨论】:

    • 您不应该在机器名称前加上 \\ - 这不会像您想象的那样工作。
    • @Mark 对,我用 %machinename% 替换了 \\machinename 以明确这只是远程主机名称的占位符。 IP 地址也可以。
    • 因为我将 \\machinename 作为参数传递给批处理文件,所以我使用了 SET test=%1, SET result=%test:*\\=% , ping -n 1 %result%。它工作..感谢您的解决方案
    猜你喜欢
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    • 2018-03-17
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 2012-02-19
    • 1970-01-01
    相关资源
    最近更新 更多