Link to home
Start Free TrialLog in
Avatar of missing_dll
missing_dllFlag for Netherlands

asked on

Check network connectivity in DOS

What is the best foolproof way in DOS (XP / Win 7)  as part of a batch script to check a) network card / adapter is active (alive) and b) connected to a network ?
Avatar of Gary Patterson, CISSP
Gary Patterson, CISSP
Flag of United States of America image

Complicated question to answer, since it is possible to have multiple NICs in a computer, and each one could be connected to a network.  Also, need to define what "connected to a network" means.  You could be plugged into a switch, and have an autoconfiguration address assigned, which may or may not meet your definition of "connected to a network" depending on how your network is defined.

One simple approach is to just ping something that you expect to be reachable.  In a corporation, maybe your local DNS server.  At home, maybe your router.  For an internet-connected system, maybe a public DNS server:

ping.exe -n 1 ip.to.test.here >nul
if errorlevel 1 goto NoConnection
...
SOLUTION
Avatar of Adam Leinss
Adam Leinss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Id suggest that if the connectivity works one can assume the adapter is up. Without connection the adapter would be unable to report back regardless that it is up. So a ping as per above suggestions should suffice.
Avatar of missing_dll

ASKER

Just tried this and the !state! is always up, even if I cannot ping the !ipaddress!, unplug the network cable

for /f "tokens=5,7" %%a in ('ping -n 1 !ipaddr!') do (
    if "x%%a"=="xReceived" if "x%%b"=="x1," set state=up
were you trying just that statement or the whole script?  The preceding line setting state=down as default,  you may need that

I tried - it worked for me
I think the problem is when I ping I still get Received packages. i.e from the ping I get "Destination host unreachable" Sent = 4, Received = 4, Lost = 0 (0% Loss)
I can see your point - I modified the code a bit -  namely,
for /f "tokens=4" %%a in ('ping -n 1 !ipaddr!') do (
if "%%a"=="bytes=32" set state=up

:: Checking adapter status
@setlocal enableextensions enabledelayedexpansion
@Echo Off
For /f "skip=2 tokens=1,2,3,*" %%a In ('NetSh Int Show Int') Do (
	if %%b==Connected call :Up "%%d"
)
Exit /B

:Up
@echo Adapter %1 is up
:: Check connectivity
set ipaddr=10.2.2.2
:loop
set state=down
for /f "tokens=4" %%a in ('ping -n 1 !ipaddr!') do (
    if "%%a"=="byte=32" set state=up
)
echo.Link is !state!
ping -n 6 127.0.0.1 >nul: 2>nul:
if !state! == up goto :mapdrives
goto :loop
endlocal
:mapdrives

Open in new window

Thanks, also I noticed that the following does not seem to work for windows xp

For /f "skip=2 tokens=1,2,3,*" %%a In ('NetSh Int Show Int') Do (
      if %%b==Connected call :Up "%%d"
)

When I ran "Netsh int show int" I don't get anything in the State column, it is just empty
Netsh was in its infancy with Windows XP.  The command syntax was a bit different than the current generation.  I don't have an XP machine accessible to test out - try "netsh int sh adapters"

You may want to try VBscript of WMI to deal with XP
Thanks found another way using WMIC