2014-12-21 16 views
1

我想檢查互聯網連接,當它失敗我想設置%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. 

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

+0

與您的問題無關,但這可能會讓您的生活更輕鬆:'for/f「tokens = *」%%我在('wmic os獲得Caption ^,OSArchitecture/value^| find「=」' )do set %% i','echo%Caption%,%OSArchitecture%' – Stephan

回答

8

通過調整,您的原始代碼將起作用。

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是TRUE,如果errorlevel爲n 或大於n更大。因此IF ERRORLEVEL 0總是如此。 IF NOT ERRORLEVEL 1是errorlevel = 0的測試。所以IF %ERRORLEVEL%==0,除了前者可以在一個塊內使用,但後者不能。

禁慾,你的代碼設置not connected但下面的行,因爲if條件總是真,覆蓋與第二個消息的價值。

注意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

+0

當我在我的代碼中執行他的結果是回聲關閉。當我創建一個新的批處理文件時,它確實可以工作。生病玩一下,看看我能做什麼。 – Ericthedreamer

+0

發現我的錯誤,它完美地工作<3 – Ericthedreamer

0

這對我有效。這個想法是檢查以「Reply」(表示成功)開頭的行的PING結果的每一行,如果它在單行上找到它,則它將%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

但是如果我嘗試在我的代碼中實現它,它不起作用。還是我在這裏做錯了? – Ericthedreamer

+0

剛剛編輯我的帖子 – Ericthedreamer

+0

檢查你的'ping'輸出。這是語言的依賴。 「回覆」可能不是「回覆」與您的窗口。 – Stephan

1

另一種選擇......

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% 

這種運作良好,因爲......

  • 有時平安返回的是Reply from 10.11.1.106: Destination host unreachable.並且由於它具有零ERRORLEVEL失敗。 (至少對我來說)
  • ping -n 2將仍然通過,如果碰巧有一個丟棄的數據包。