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

File 用于检查internet连接并根据结果设置环境变量%internet%的简单批处理,file,batch-file,ping,File,Batch File,Ping,我想检查internet连接,当连接失败时,我想将%internet%设置为未连接。如果它对连接的有效 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) 我也试过: @echo off c

我想检查internet连接,当连接失败时,我想将
%internet%
设置为未连接。如果它对连接的
有效

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)
我也试过:

@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
---------------编辑------------

这是更多的代码

@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.
-----------------------------编辑3-----------------

完成

这对我很有效。其思想是,它检查
PING
结果的每一行中以“Reply”(表示成功)开头的行,如果它在一行中找到它,则将
%Connected%
变量设置为
true

@ECHO OFF

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 the internet.
) ELSE (
    SET Internet=Not connected to the internet.
)

通过调整,您的原始代码将正常工作

echo检查互联网连接
Ping www.google.nl-n1-w1000
cls
如果错误级别为1(设置internet=未连接到internet)或者(设置internet=已连接到internet)
回音%internet%
问题是如果
ERRORLEVEL
为n或大于n,则
IF ERRORLEVEL n
为真<如果ERRORLEVEL 0因此始终为真,则代码><代码>如果不是ERRORLEVEL 1,则测试ERRORLEVEL=0。如果%ERRORLEVEL%==0,则
也是如此,但前者可以在块内使用,但后者不能

Nence,您的代码设置了
未连接
,但由于
如果
条件始终为真,则下一行将用第二条消息覆盖该值

请注意,
set“%internet%”==“Connected to internet”
表示“将变量的名称存储在变量
internet
中,设置为值”

因此,如果当时
internet
的值为
fred
,则
fred
将被设置为该值,而不是
internet

此外,在块语句
(括号内的一系列语句)
,整个块被解析并执行。在解析块时,块内的任何
%var%
都将替换为该变量的值——在执行块之前——这同样适用于。。。DO(块)

因此,由于您正在使用一个块设置
internet
if-true
语句序列),因此您需要使用两种常见方法之一来克服此问题。1)使用
setlocal enabledelayedexpansion
并使用
!瓦尔
代替
%var%
以访问
var
的更改值,或2)调用子例程以使用更改的值执行进一步处理。技术上与第二种方法相关的一种方法是使用语句
call echo%%internet%%
在块内显示
internet
的更改值。

另一个选项

ping -n 2 -w 700 10.11.1.1 | find "bytes="
IF %ERRORLEVEL% EQU 0 (
    SET internet=Connected to the internet.
) ELSE (
    SET internet=Not connected to the internet.
)
echo %internet%
这很有效,因为

  • 有时ping的返回是10.11.1.106:目标主机的回复 无法访问。由于它的errorlevel为零,因此失败。(至少对我来说)
  • ping-n2
    如果碰巧有丢弃的数据包,仍然会通过

    • 使用ping测试互联网连接不是一个好主意。 几个防火墙阻止ping数据包。我建议使用官方的MS命令行实用程序portqry。

      但是如果我试图在代码中实现它,它就不起作用了。还是我做错了?检查您的
      ping
      输出。这取决于语言。“回复”在您的windows中可能不是“回复”。@user2991298-您使用它的方式有点不正确。我更新了我的答案,使用If-Else结构,而不是Goto-这更符合您正在做的事情。试着实现它,它应该会起作用。与您的问题无关,但这可能会让您的生活更轻松:
      for/f“tokens=*”%%i in('wmic os get Caption^,OSArchitecture/value^ find“=”)设置%%i
      echo%Caption%,%OSArchitecture%
      当我在我的代码中实现his时,结果是echo关闭了。当我刚创建一个新的批处理文件时,它确实可以工作。我玩一会儿,看看能做些什么。