2015-09-22 71 views
-5
@echo off 

set /p host= 

for /f "delims==< tokens=4" %%b in ('ping %host% -n 1 ^| findstr "Reply"') do (set ttl=%%b) 

if '%ttl%' GTR "64" (echo operating system: Windows - ttl=%ttl%) else (echo operating system: linux - ttl=%ttl%) 

pause 

爲什麼這個批處理文件無法正常工作?爲什麼這個批處理文件不起作用?

+3

它應該做什麼,它在做什麼? – SomethingDark

+0

因爲有錯誤。請提出具體問題以獲得具體答案(閱讀SO幫助,並查看有關如何編寫好問題的部分);你的問題在這裏是題外話題... – aschipfl

回答

1
@echo off 
set /p host= 
set "ttl=" 
for /f "delims==< tokens=4" %%b in (' 
    ping "%host%" -4 -n 1 ^| findstr /I "TTL" 
') do (set /A "ttl=%%b") 
if defined ttl (
    if %ttl% GTR 64 (
    echo operating system: Windows - ttl=%ttl% 
) else (
    echo operating system: linux - ttl=%ttl% 
) 
) else echo unknown "%host%" host 

的變化:

  • set "ttl="到空/刪除ttl變量,看下if defined ttl;
  • ping "%host%" -4 -n 1
    • "%host%"作爲當前書面,%host%計算結果可能爲空字符串和ping ""結果ping "%COMPUTERNAME%";
    • -4強制IPv4ping ""默認爲IPv6;
  • findstr /I "TTL"儘可能Reply from a.b.c.d: Destination host unreachable;
  • set /A "ttl=%%b"用於下一個比較;
  • if %ttl% GTR 64(試行結果:'' GTR "64"'1' GTR "64"真正等);
  • if defined ttl
相關問題