2015-09-22 31 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
相关问题