在批处理文件中使用 ping 来确定最快的服务器

发布于 2024-12-13 06:32:09 字数 321 浏览 1 评论 0原文

我想对我们公司网络中的 2 台服务器执行 ping 操作,并确定哪一个速度最快(基于 ping 的最小 = [n] 毫秒)。然后使用最快的服务器做一些事情。

伪代码:

a = Ping MyServerA
b = Ping MyServerB
if (a < b)
    copy from x to c:\
else
    copy from y to c:\

PS 请输入一个工作脚本,因为我真的不太了解批处理文件,例如:“for /F”tokens=3“%%a in (reg2.txt) do set MyDocs=%%a”

I want to ping 2 servers in our company network, and determine which is the fastest (based on the Minimum = [n]ms at the and of a ping). Then do something using the fastest server.

Pseudo code:

a = Ping MyServerA
b = Ping MyServerB
if (a < b)
    copy from x to c:\
else
    copy from y to c:\

P.S. Please type out a working script, because i really do not understand much about batch file things like: "for /F "tokens=3" %%a in (reg2.txt) do set MyDocs=%%a"

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

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

发布评论

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

评论(1

夏九 2024-12-20 06:32:09

此代码在 FastestServer 变量中获取最快的服务器,因此您可以在任何您希望的命令中使用它,即 copy %FastestServer% ...

@echo off
set MyServerA=127.0.0.1
set MyServerB=127.0.0.2
for /f "tokens=2 delims==," %%a in ('ping %MyServerA% -n 1 -w 1000') do set MinServerA=%%a
for /f "tokens=2 delims==," %%b in ('ping %MyServerB% -n 1 -w 1000') do set MinServerB=%%b
set MinServerA=%MinServerA:~0,-2%
set MinServerB=%MinServerB:~0,-2%
if %MinServerA% lss %MinServerB% (
    set FastestServer=%MyServerA%
    ) else (
    set FastestServer=%MyServerB%
)
echo Fastest Server: %FastestServer%

我希望代码是不言自明的并且将按预期工作。如果您还有任何疑问,请发布!

This code get the fastest server in FastestServer variable, so you may use it in any command you wish, i.e. copy %FastestServer% ...:

@echo off
set MyServerA=127.0.0.1
set MyServerB=127.0.0.2
for /f "tokens=2 delims==," %%a in ('ping %MyServerA% -n 1 -w 1000') do set MinServerA=%%a
for /f "tokens=2 delims==," %%b in ('ping %MyServerB% -n 1 -w 1000') do set MinServerB=%%b
set MinServerA=%MinServerA:~0,-2%
set MinServerB=%MinServerB:~0,-2%
if %MinServerA% lss %MinServerB% (
    set FastestServer=%MyServerA%
    ) else (
    set FastestServer=%MyServerB%
)
echo Fastest Server: %FastestServer%

I hope the code be self-explanatory and will work as expected. If you have any further question, just post it!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文