Skip to main content

How to Check for an Active Internet Connection in a Batch Script

Many scripts rely on an active internet connection to perform their tasks, such as downloading files, accessing a network share, or communicating with an API. If a script runs without a connection, it will fail with network errors. A robust script should perform a "pre-flight check" to verify that an internet connection is available before it attempts any network operations.

This guide will teach you the most common and reliable method for checking for an internet connection by using the PING command to test connectivity to a well-known, highly available server like one of Google's public DNS servers.

The Challenge: No Direct "IsOnline" Command

There is no single command in batch scripting like IF INTERNET.AVAILABLE. A script cannot simply ask the operating system if it's "online." For example, a computer can be connected to a local network (and have an IP address) but have no connection to the wider internet.

Therefore, the only reliable way to check for a true internet connection is to attempt to contact a server that you know is on the internet.

The Core Method: Pinging a Reliable External Host

The PING command is a universal network utility that sends a small data packet (an ICMP echo request) to a target host and waits for a reply. If it receives a reply, it means there is a working network path between your computer and the target.

The Logic: If we can successfully PING a major, highly reliable public server, we can be confident that our internet connection is working.

Good Targets to PING:

  • 8.8.8.8 or 8.8.4.4: Google's Public DNS servers. These are excellent choices as they are fast, extremely reliable, and memorable.
  • 1.1.1.1 or 1.0.0.1: Cloudflare's Public DNS servers. Also an excellent choice.

It is generally better to ping an IP address than a domain name (like google.com) because it removes DNS resolution as a potential point of failure.

Example: A Basic Internet Connection Check

This script uses PING to check for a connection and reports the status based on the command's exit code.

@ECHO OFF
SET "TARGET_HOST=8.8.8.8"

ECHO --- Checking for an active internet connection ---
ECHO Pinging %TARGET_HOST% to test connectivity...
ECHO.

REM -n 1: Send only 1 echo request.
REM > NUL: Suppress the output of the PING command.
PING -n 1 "%TARGET_HOST%" > NUL

IF %ERRORLEVEL% EQU 0 (
ECHO [SUCCESS] Internet connection is active.
) ELSE (
ECHO [FAILURE] No internet connection, or the target host is unreachable.
)

How the PING Check Works

The PING command communicates its result via its exit code (%ERRORLEVEL%).

  • ERRORLEVEL 0: Success. A reply was received from the target host. This confirms the connection is working.
  • ERRORLEVEL 1: Failure. No reply was received. This indicates a problem (no internet, a firewall is blocking the ping, or the target is down).

Our script's logic is a simple IF %ERRORLEVEL% EQU 0 check. We use > NUL to hide the detailed output of the PING command itself, as we only care about the final success or failure.

Common Pitfalls and How to Solve Them

The Target Host is Down

While highly unlikely for major public DNS servers, it is theoretically possible that the server you are pinging could be temporarily offline.

Solution: For mission-critical scripts, you can build in redundancy by checking a second target if the first one fails.

PING -n 1 8.8.8.8 > NUL
IF %ERRORLEVEL% EQU 0 GOTO :IsOnline

ECHO Primary target failed. Pinging secondary target...
PING -n 1 1.1.1.1 > NUL
IF %ERRORLEVEL% EQU 0 GOTO :IsOnline

GOTO :IsOffline

:IsOnline
ECHO Internet connection is active.
...

Firewall Issues

Some corporate or public network firewalls are configured to block ICMP traffic, which is what the PING command uses. In such an environment, your PING test will fail even if you have a working internet connection for web browsing (which uses TCP on ports 80/443).

Solution: In this advanced and rare scenario, a PING test is not reliable. A more robust (and complex) check would be to use PowerShell to attempt a TCP connection to a web server on port 80.

powershell -Command "try { (New-Object Net.Sockets.TcpClient).Connect('www.google.com', 80) } catch { exit 1 }"
IF %ERRORLEVEL% EQU 0 (ECHO Connection OK) ELSE (ECHO Connection FAILED)

For 99% of use cases, however, the PING method is sufficient and much simpler.

Practical Example: A Pre-Download Check

This script needs to download a setup file from the internet. Before it starts, it uses the PING method to verify that a connection is available.

@ECHO OFF
SETLOCAL
SET "DOWNLOAD_URL=https://example.com/setup.exe"
SET "OUTPUT_FILE=setup.exe"

ECHO --- Application Downloader ---
ECHO.

ECHO Step 1: Verifying internet connection...
PING -n 1 8.8.8.8 > NUL
IF %ERRORLEVEL% NEQ 0 (
ECHO [ERROR] No internet connection detected.
ECHO Please connect to the internet and try again.
GOTO :End
)

ECHO [SUCCESS] Internet connection is active.
ECHO.
ECHO Step 2: Starting download...
REM The BITSADMIN command is just an example for a download.
REM BITSADMIN /TRANSFER "MyDownloadJob" "%DOWNLOAD_URL%" "%OUTPUT_FILE%"

ECHO Download command issued.

:End
ENDLOCAL

Conclusion

The PING command provides a simple, fast, and reliable method for checking for an active internet connection from a batch script.

Key takeaways for a robust check:

  • Ping a highly reliable public IP address, such as Google's DNS (8.8.8.8) or Cloudflare's (1.1.1.1).
  • Use the syntax PING -n 1 <IP_ADDRESS> > NUL to perform a single, silent check.
  • Check the result using IF %ERRORLEVEL% EQU 0 (Success) or IF %ERRORLEVEL% NEQ 0 (Failure).
  • Be aware that some restrictive firewalls may block PING, but for most environments, it is the perfect tool for the job.