I'm trying to create batch file to connect to a home
network appliance storage (NAS). How do I make it to
first ping if NAS is present to "net use: ", if not
present echo NAS is unreachable?@echo off
ping -n 1 192.168.1.226 rem My_NAS
if ALIVE goto :CONNECT
if UNREACHABLE goto :END:CONNECT
if exist p: goto drivefound0 else
goto end
:drivefound0
net use p: /del
:endif exist s: goto drivefound1
goto end
:drivefound1
net use s: /del
:endnet use p: \\airnas\user1 password1 /user:airnas\user1
net use s: \\airnas\public:END
echo DONE!
@echo off
ping -n 1 192.168.1.226 | find "Reply" > nul
if errorlevel 1 goto :eof
goto :CONNECT
=====================================
Helping others achieve escape felicityM2
M2, two problems with this call: ping -n 1 192.168.1.226 | find "Reply" > nul
1. It does not work if you have Windows in another language ?!
2. If MS decides to change the output text ("Reply from ") it'll fail as wellThe chance any of the above from happening, is larger than the chance they will decide to change the errorlevel return codes ... so :
@echo off
set machine=192.168.1.226
ping -n 1 %machine% > nul
if errorlevel 1 goto :failure
goto :CONNECT:connect
echo Connection to %machine% is OK!
goto :EOF:failure
echo Connection to %machine% failed...
goto :EOF
[1] XP seems unlikely to just fall out of bed and start putting out diffreent verbage.. [2] If OP uses another language, with a little effort she/he can tailor to suit.
[3] ping returns non 0 only if it fails to resolve.
=====================================
Helping others achieve escape felicityM2
Thanks, tvc... This really works with slight correction... set machine=192.168.1.226
ping -n 1 %machine% > nul
if errorlevel 1 goto :failure
goto :CONNECT
:connect
echo Connection to %machine% is OK!
goto :CONNECT
:failure
echo Connection to %machine% failed...
goto :EOFAlso my
if exist p: goto drivefound0 else
goto end
:drivefound0
net use p: /del
:endif exist s: goto drivefound1
goto end
:drivefound1
net use s: /del
:enddont work or it doesn't disconnect the p: and s: drive, instead
I changed it to passive straight...net use p: /del
net use s: /delAny idea?
Yes (14) | ![]() | |
No (14) | ![]() | |
I don't know (15) | ![]() |