missing_dll
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 ?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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
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 tried - it worked for me
ASKER
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
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
ASKER
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
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
You may want to try VBscript of WMI to deal with XP
ASKER
Thanks found another way using WMIC
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
...