简体   繁体   English

Ping测试批处理文件不起作用?

[英]Ping test batch file not working?

I have been working on a simple little project, but the batch code refuses to work. 我一直在做一个简单的小项目,但是批处理代码拒绝工作。 Can anyone help fix this? 谁能帮忙解决此问题? It either tells me that the ip must be specified or the ping result just doesn't show up. 它要么告诉我必须指定ip,要么不显示ping结果。

@echo off

echo       :1 Minecraft server resolver 
echo       :2 Website resolver
echo       :3 Ping test
echo       :4 Crash this computer
echo       Please enter your selection 
set /p whatapp=
cls

if %whatapp%==1 (
    echo         Please enter the IP of the Minecraft server you wish to resolve   
    set /p x=IP=
    set n=1
    PING %x% -n 1
    call :Pingtest 
    pause > nul
:Pingtest
    IF %errorlevel% EQU 1 (echo Server is Offline) else (GOTO:EOF)
    pause
) else if %whatapp%==2 (
    codetoinstallapp2
) else (
    echo invalid choice
)
  1. EnableDelayedExpansion EnableDelayedExpansion

Delayed Expansion will cause variables to be expanded at execution time rather than at parse time, this option is turned on with the SETLOCAL command. 延迟扩展将导致变量在执行时而不是在解析时进行扩展,此选项已通过SETLOCAL命令打开。 When delayed expansion is in effect variables can be referenced using !variable_name! 当延迟扩展生效时,可以使用!variable_name!引用!variable_name! (in addition to the normal %variable_name% ) (除了正常的%variable_name%

  1. Mixed CALL :Pingtest with reaching the :Pingtest label by normal code pass. 混合CALL :Pingtest ,并通过常规代码通过达到:Pingtest标签。

  2. Using GOTO or even :label within parentheses - including FOR and IF commands - will break their context . 在括号内使用GOTO 或什至:label label-包括FORIF命令- 将破坏其上下文

  3. A successful/unsuccessful PING does NOT always return an %errorlevel% of 0 / 1 . 一个成功/失败的PING并不总是返回%errorlevel%0 / 1 Therefore to reliably detect a successful ping - pipe the output into FIND and look for the text " TTL " 因此, 为了可靠地检测到成功的ping操作 ,请将输出通过管道传送FIND并查找文本“ TTL

Hence, use 因此,使用

@echo OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
color 02
echo(---
echo       :1 Minecraft server resolver 
echo       :2 Website resolver
echo       :3 Ping test
echo       :4 Crash this computer
echo       Please enter your selection 
set /p whatapp=
cls

if %whatapp%==1 (
  cls
  color 02
  echo( ---
  echo         Please enter the IP of the Minecraft server you wish to resolve   
  set /p x=IP=
  set n=1
  PING !x! -n 1|FIND /I "TTL="
REM   call :Pingtest 
REM   pause > nul
REM   :Pingtest
  echo !errorlevel!
  IF !errorlevel! EQU 1 (echo Server !x! is Offline) else (
    echo Server !x! is Online
    rem next code here
    REM GOTO:EOF
  )
  pause

) else if %whatapp%==2 (
  codetoinstallapp2
) else (
  echo invalid choice
)

You need to enable delayed expansion for your code to work since you are assigning and reading a variable inside of a code block: 由于要在代码块中分配和读取变量,因此需要启用延迟扩展才能使代码正常工作:

setlocal EnableDelayedExpansion

if %whatapp%==1 (
    set /p x=IP=
    ping !x! -n 1
)
endlocal

In addition you need to move the :PINGTEST section outside of the if %whatapp% block to the very end of your script, and place a goto :EOF before it to not fall into it unintentionally. 另外,您需要将:PINGTEST部分移至if %whatapp%块之外,然后if %whatapp%脚本的末尾,并在其前面放置一个goto :EOF ,以免意外插入该区域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM