2011-11-01 46 views
0

我想在我們的公司網絡中ping 2臺服務器,並確定哪個最快(基於ping和ping的Minimum = [n] ms)。然後用最快的服務器做一些事情。在批處理文件中使用ping來確定最快的服務器

僞代碼:

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

附:請輸入一個工作腳本,因爲我真的不太瞭解批處理文件,比如:「for/F」tokens = 3「%% a in(reg2.txt)do set MyDocs = %% a」

回答

3

此代碼獲得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% 

我希望的代碼是不言自明的,並會正常運行。如果您還有其他問題,請發佈!

相關問題