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

问题描述 投票:0回答:6

我想检查互联网连接,当它失败时我想将

%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)

我也尝试过这个:

@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 ------------------
完成

file batch-file ping
6个回答
15
投票

经过调整,你的原始代码就可以工作了。

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%

问题是,如果

IF ERRORLEVEL n
是 n
 或大于 n
,则 errorlevel 为 TRUE。因此
IF ERRORLEVEL 0
始终为真。
IF NOT ERRORLEVEL 1
是 errorlevel=0 的测试。
IF %ERRORLEVEL%==0
也是如此,只不过前者可以在块内使用,而后者不能。

Nence,您的代码 was 设置

not connected
但下面的行,由于
if
条件始终为 true,会用第二条消息覆盖该值。

注意

set "%internet%"=="Connected to internet"
的意思是“设置一个变量,其名称存储在变量
internet
中为值”

因此,如果当时

internet
的值为
fred
,那么
fred
将被设置为该值,而不是
internet

此外,在块语句

(a parenthesised series of statements)
内,会解析并执行整个块。块内的任何 %var% 都将被该变量的值替换
在解析块时 
- 在执行块之前 - 同样的事情也适用于 FOR ... DO (block)
因此,由于您使用块(

internet

语句序列)设置

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


7
投票

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%

这很有效,因为...

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

1
投票
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.
)



0
投票
https://support.microsoft.com/it-it/help/310099/description-of-the-portqry-exe-command-line-utility


0
投票
ERRORLEVEL

设置的

ping
是徒劳的,因为在某些情况下,即使没有从远程主机收到任何有效回复,它也会将
ERRORLEVEL
设置为
0
(成功)!!! 但是,下面描述的方法是可靠的、可移植的并且适用于:

翻译成所有其他语言,例如:“ms”、“TTL”、“回复”、“丢失”、“无法到达”、“已收到”等...,包括俄语和中文。
  1. Ping IPv4 地址。
  2. Ping IPv6 地址(无
  3. TTL
  4. 字段)
    发生错误
  5. “一般故障”
  6. 的情况。 Ping 一个地址,返回
  7. “请求超时。”
  8. Ping 地址时返回
  9. “目标主机无法访问。”
  10. Ping 可解析的字母数字主机。
  11. Ping 无法解析的字母数字主机,这会导致返回
  12. “Ping 请求无法找到主机 UnresolvableHost...”
  13. 错误。
  14. @echo off for /F %%A in ('ping %1 -n 1 ^| findstr /C:^= ^| find /c /v ""') do (set "res=%%A") if %res% GEQ 3 ( set internet=Connected to internet ) else ( set internet=Not connected to internet )
如果将上面的代码保存到名为例如
pingtest.bat

,你可以这样使用它:

pingtest google.com

此方法利用了字符串中存在的等号 
bytes=

,这些等号不受翻译为其他语言的影响,并且不会出现在不成功的 ping/echo 结果中。

此外,将开关

-n 1

修改为例如

-n 6
允许您在变量
%res%
中获得反映连接质量的定性结果。
使用下面的代码,您可以标准化变量

%res%

中的结果,使其在失败时变为

0
...以及在成功时 - 成功 ping 的数量:
@echo off

for /F %%A in ('ping %1 -n 6 ^| findstr /C:^= ^| find /c /v ""') do (set "res=%%A")

if %res% GEQ 3 (
    set /A "res=%res%-2"
) else (
    set "res=0"
)

echo The number of successful pings is %res%

要暂停程序直到成功 ping 通主机,只需像这样循环调用它:

@echo off :loop for /F %%A in ('ping %1 -n 1 ^| findstr /C:^= ^| find /c /v ""') do (set "res=%%A") if %res% LSS 3 goto :loop



-1
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.