Skip navigation

JSI Tip 7496. How can a script determine if the computer is connected to the internet?


I have scripted Internet.bat to allow your script to determine if the computer is connected to the Internet.

The syntax for using Internet.bat is:

call internet Var, where Var is a call directed environment variable that will contain:

N if the computer does NOT have a working connection to the internet.

D if the internet could be reached with a name query.

I if the internet could only be reached via an IP address.

Internet.bat contains:

@echo off
if \{%1\}==\{\} @echo Syntax: call internet Connected&goto :EOF
setlocal
set connected=N
:: Try name lookups
for /f "Tokens=*" %%c in ('ping -n 1 www.apnic.net ^| FIND "TTL"') do set connected=D&goto finish
for /f "Tokens=*" %%c in ('ping -n 1 www.arin.net ^| FIND "TTL"') do set connected=D&goto finish
for /f "Tokens=*" %%c in ('ping -n 1 lacnic.net ^| FIND "TTL"') do set connected=D&goto finish
for /f "Tokens=*" %%c in ('ping -n 1 www.ripe.net ^| FIND "TTL"') do set connected=D&goto finish
:: Try using a IP address
for /f "Tokens=*" %%c in ('ping -n 1 202.12.29.20 ^| FIND "TTL"') do set connected=I&goto finish
for /f "Tokens=*" %%c in ('ping -n 1 192.149.252.16 ^| FIND "TTL"') do set connected=I&goto finish
for /f "Tokens=*" %%c in ('ping -n 1 200.160.2.15 ^| FIND "TTL"') do set connected=I&goto finish
for /f "Tokens=*" %%c in ('ping -n 1 193.0.0.203 ^| FIND "TTL"') do set connected=I&goto finish
:finish
endlocal&set %1=%connected%



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish