batch to ping host if present proceed

Microsoft Windows xp professional editio...
January 27, 2010 at 23:34:16
Specs: Windows XP
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
:end

if exist s: goto drivefound1
goto end
:drivefound1
net use s: /del
:end

net use p: \\airnas\user1 password1 /user:airnas\user1
net use s: \\airnas\public

:END
echo DONE!


See More: batch to ping host if present proceed

Report •

#1
January 28, 2010 at 01:37:58
@echo off
ping -n 1 192.168.1.226 | find "Reply" > nul
if errorlevel 1 goto :eof
goto :CONNECT


=====================================
Helping others achieve escape felicity

M2


Report •

#2
January 28, 2010 at 02:25:20
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 well

The 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



Report •

#3
January 28, 2010 at 03:12:05
[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 felicity

M2


Report •

Related Solutions

#4
January 28, 2010 at 15:02:13
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 :EOF

Also my
if exist p: goto drivefound0 else
goto end
:drivefound0
net use p: /del
:end

if exist s: goto drivefound1
goto end
:drivefound1
net use s: /del
:end

dont 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: /del

Any idea?


Report •

Ask Question