用于检查互联网连接并根据结果设置环境变量%internet%的简单批处理

Eri*_*mer 3 file ping batch-file

我想检查互联网连接,当它失败时我想设置%internet%为未连接.如果它奏效了connected.

echo checking internet connection
Ping www.google.nl -n 1 -w 1000
cls
if errorlevel 1 (set internet=Not connected to internet)
if errorlevel 0 (set internet=Connected to internet)
Run Code Online (Sandbox Code Playgroud)

我也试过这个:

@echo off
cls
echo Checking connection
ping -n 1 www.google.com >nul
if errorlevel 1 (
  cls
  set "%internet%"=="Not connected to internet"
  echo %internet%
  pause>nul
  exit
)

cls
set "%internet%"=="Connected to internet"
echo %internet%
pause>nul
pause
Run Code Online (Sandbox Code Playgroud)

- - - - - - - -编辑 - - - - - -

这是更多的代码.

@echo off
set versienummer=v3.1
title AutoMatic Program Install Stable %versienummer% by eric
color 0a
:CheckOS 
IF "%PROCESSOR_ARCHITECTURE%"=="x86" (set bit=x86) else (set bit=x64)
set "windows="
VER | find  " 5.1." > nul && set windows=XP
VER | find  " 5.2." > nul && set windows=XP 64-Bit or Server 2003 or Server 2003 R2 
VER | find  " 6.0." > nul && set windows=Vista or server 2008
VER | find  " 6.1." > nul && set windows=Win7 or server 2008 R2
VER | find  " 6.2." > nul && set windows=Windows 8
VER | find  " 6.3." > nul && set windows=Server 2012 R2 or Windows 8.1
if defined windows (
echo %windows%
) else (
echo unknown operating system
)
:ReturnToBaseLine



echo checking internet connection
Ping www.google.nl -n 1 -w 1000
cls
if errorlevel 1 (set internet=Not connected to internet)
if errorlevel 0 (set internet=Connected to internet)
SET Connected=false
FOR /F "usebackq tokens=1" %%A IN (`PING google.com`) DO (
REM Check the current line for the indication of a successful connection.
IF /I "%%A"=="Reply" SET Connected=true
)
REM Check if a success was found.
IF "%Connected%"=="true" SET internet=Connected to internet

REM If we get here, we are not connected.
set internet=Not connected to internet
pause

REM Quit.



color 0a
cls
echo Made By The Amazing 
echo.
echo    ____    _       ________         ___                            
echo   / __/___(_)___  /_  __/ /  ___   / _ \_______ ___ ___ _  ___ ____
echo  / _// __/ / __/   / / / _ \/ -_) / // / __/ -_) _ `/  ' \/ -_) __/
echo /___/_/ /_/\__/   /_/ /_//_/\__/ /____/_/  \__/\_,_/_/_/_/\__/_/   
echo.                                                                   
Echo     %bit% processor architecture           versie %versienummer%
echo     %windows%
echo     %internet%
echo.
Run Code Online (Sandbox Code Playgroud)

----------------------------- edit 3 ------------------
DONE

Mag*_*goo 10

通过调整,您的原始代码将起作用.

echo checking internet connection
Ping www.google.nl -n 1 -w 1000
cls
if errorlevel 1 (set internet=Not connected to internet) else (set internet=Connected to internet)

echo %internet%
Run Code Online (Sandbox Code Playgroud)

IF ERRORLEVEL n如果errorlevel是n 或大于n,则问题是TRUE .IF ERRORLEVEL 0因此总是如此.IF NOT ERRORLEVEL 1是errorlevel = 0的测试.所以IF %ERRORLEVEL%==0,除了前者可以在一个区块内使用,但后者不能.

Nence,您的代码正在设置not connected但是以下行,因为if条件始终为true,所以使用第二条消息覆盖该值.

注意,这set "%internet%"=="Connected to internet"意味着"设置一个变量,其名称存储在变量internet中的值"

因此,如果internet有一个当时的值fred,那么fred将被设置为值,而不是internet.

此外,在块语句中(a parenthesised series of statements),整个块被解析然后执行.%var%块中的任何内容都将被解析块时的变量值替换- 在块执行之前 - 同样的事情适用于a FOR ... DO (block).

因此,由于要设置internet一个块(if - true语句序列,那么你需要使用的两种常用方法之一来克服这一点.1)使用setlocal enabledelayedexpansion和使用!var!到位的%var%访问的改变的值var或者2)调用子程序使用更改的值执行进一步处理.技术上与第二种方法相关的方法是使用该语句call echo %%internet%%来显示internet块内的改变值.


Sun*_*est 6

另外一个选择...

ping -n 2 -w 700 10.11.1.1 | find "TTL="
IF %ERRORLEVEL% EQU 0 (
    SET internet=Connected to the internet.
) ELSE (
    SET internet=Not connected to the internet.
)
echo %internet%
Run Code Online (Sandbox Code Playgroud)

这很有效,因为...

  • 有时 ping 的返回结果是Reply from 10.11.1.106: Destination host unreachable.且由于错误级别为零,因此会失败。(至少对于我来说)
  • ping -n 2如果碰巧有丢包,仍然会通过。