noodleNT
asked on
Batch file - Check if Machine is online first
I have a list of computers dumpted from AD. I then call that up that file and want to test if the machine is online or not. I was trying to use Net View \\Computer but would get an error 53 if the machine wasn't online. Is there any way I can do a simple test for that?
If error = 53 then Set Online=False
Something like that.
This is my current code:
************************** ********** ********** ***
For /F "delims=" %%i in (%FileInput%) do (
Echo Computer: %%i
set /a LineCount=+1
Echo LineCount: %LineCount%
ECHO. >>C:\Scripts\xcacls\LOG_MS FIX.txt
ECHO. >>C:\Scripts\xcacls\LOG_MS FIX.txt
ECHO ************************** ********** ********** ********** >>C:\Scripts\xcacls\LOG_MS FIX.txt
ECHO COMPUTER: %%i >>C:\Scripts\xcacls\LOG_MS FIX.txt
ECHO ************************** ********** ********** ********** >>C:\Scripts\xcacls\LOG_MS FIX.txt
For /F "Tokens=1" %%a in ('Net view "\\%%i"') do set Online=%%a
Echo Online="%online%"
IF "%Online%" == "error" (
ECHO %%i did not respond at %Time% >>C:\Scripts\xcacls\LOG_MS FIX.txt
) Else (
ECHO %%i ALIVE! at %Time% >>C:\Scripts\xcacls\LOG_MS FIX.txt
)
)
If error = 53 then Set Online=False
Something like that.
This is my current code:
**************************
For /F "delims=" %%i in (%FileInput%) do (
Echo Computer: %%i
set /a LineCount=+1
Echo LineCount: %LineCount%
ECHO. >>C:\Scripts\xcacls\LOG_MS
ECHO. >>C:\Scripts\xcacls\LOG_MS
ECHO **************************
ECHO COMPUTER: %%i >>C:\Scripts\xcacls\LOG_MS
ECHO **************************
For /F "Tokens=1" %%a in ('Net view "\\%%i"') do set Online=%%a
Echo Online="%online%"
IF "%Online%" == "error" (
ECHO %%i did not respond at %Time% >>C:\Scripts\xcacls\LOG_MS
) Else (
ECHO %%i ALIVE! at %Time% >>C:\Scripts\xcacls\LOG_MS
)
)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Well, I just ran a few tests and this works exactly as I expected it would.
I suggest you might have problems with DNS if your ping of a name fails. In an Active Directory environment all your workstations should be in AD DNS and hence pinging a name should work fine.
In my tests, REPLY always got a 0 and timed out and not found got a 1 (as error level) this is on XP SP2.
I suggest you might have problems with DNS if your ping of a name fails. In an Active Directory environment all your workstations should be in AD DNS and hence pinging a name should work fine.
In my tests, REPLY always got a 0 and timed out and not found got a 1 (as error level) this is on XP SP2.
ASKER
These are all laptops. So they may be at the office or not. If they are not on the network DNS will not be able to resolve their names... because the don't exist.
DNS doesn't just drop their names if they aren't on the network. My laptop hasn't been connected to my network since Thursday, but it was what I tested the "request timed out" with Unless you've modified your DNS to get rid of "old" entries VERY quickly.
Thing is, if you are able to "NET VIEW" them, they should be in DNS long enough to resolve.
Thing is, if you are able to "NET VIEW" them, they should be in DNS long enough to resolve.
ASKER
I have the same problem with net view.
I am now dumping the resolts to a txt file. I then use find to test for the string "could not find host". This works but not sure how to set variables off of it.
Something like this???
For /F "delims= Tokens=2" %%a in ('Find /C "could not find host" C:\Scripts\xcacls\Ping_Tes t.txt') do (
Echo A=%%a
Set online=%%a
)
I am now dumping the resolts to a txt file. I then use find to test for the string "could not find host". This works but not sure how to set variables off of it.
Something like this???
For /F "delims= Tokens=2" %%a in ('Find /C "could not find host" C:\Scripts\xcacls\Ping_Tes
Echo A=%%a
Set online=%%a
)
ASKER
Got it to work...
@Echo Off
ECHO ************************** **
ECHO *** FIX MS PATCH ****
ECHO *** v 2.0 ****
ECHO ************************** **
:FilePrompt
Echo Enter the name of the saved text file with the computer names.
Set /P FileInput=Input Source:
For /F "delims=. Tokens=1,2" %%a in ("%FileInput%") Do (
Set FileName=%%a
Set Filetype=%%b
)
If "%Filetype%"=="" (
Echo Please type the full name of the file.
Goto :FilePrompt
)
:PassPrompt
Echo Please enter the Domain\Admin password.
Set /P DomPass=Domain Admin Password:
If "%DomPass%"=="" (
ECHO Please enter password.
Goto :PassPrompt
)
type %FileInput% > FileFix.reg
type FileFix.reg > %FileInput%
Del /Q FileFix.reg
For /F "delims=" %%i in (%FileInput%) do Call :Online %%i
Goto :EOF
:ONline
set Machine=%~1
set Machine=%Machine: =%
set RESULT=
Echo Computer:%Machine%
CD C:\Scripts\xcacls
If NOT "%Machine%" == "" (
ECHO. >>C:\Scripts\xcacls\LOG_MS FIX.txt
ECHO. >>C:\Scripts\xcacls\LOG_MS FIX.txt
ECHO ************************** ********** ********** ********** >>C:\Scripts\xcacls\LOG_MS FIX.txt
ECHO COMPUTER: %Machine% >>C:\Scripts\xcacls\LOG_MS FIX.txt
ECHO ************************** ********** ********** ********** >>C:\Scripts\xcacls\LOG_MS FIX.txt
REM ping %Machine% -n 4 -w 100 >Ping_Test.txt
REM findstr /C:"find" Ping_Test.txt
ping -w 1000 %MACHINE% |findstr /r /c:"Reply from" 1>NUL 2>NUL
if errorlevel 1 goto Return_False
if errorlevel 0 goto Return_True
)
:Return_True
set RESULT=Successful Ping
Goto Run_Xcacls
:Return_False
Set RESULT=Failed to Connect
Goto Run_Xcacls
:Run_Xcacls
Echo Result:%Result%
if NOT "%RESULT%" == "Successful Ping" (
ECHO %MAchine% did not respond at %Time% >>C:\Scripts\xcacls\LOG_MS FIX.txt
) Else (
ECHO %Machine% ALIVE! at %Time% >>C:\Scripts\xcacls\LOG_MS FIX.txt
cscript.exe /h:cscript
Xcacls \\%MAchine%\c$\Windows\reg istration /G everyone:R system:F administrators:F /user Domain\admin /pass %DomPass% /L C:\Scripts\xcacls\LOG_MSFI X.txt
Xcacls \\%Machine%\c$\Windows\reg istration\ *.clb /G everyone:R system:F administrators:F /user Domain\admin /pass %DomPass% /L C:\Scripts\xcacls\LOG_MSFI X.txt
)
:EOF
@Echo Off
ECHO **************************
ECHO *** FIX MS PATCH ****
ECHO *** v 2.0 ****
ECHO **************************
:FilePrompt
Echo Enter the name of the saved text file with the computer names.
Set /P FileInput=Input Source:
For /F "delims=. Tokens=1,2" %%a in ("%FileInput%") Do (
Set FileName=%%a
Set Filetype=%%b
)
If "%Filetype%"=="" (
Echo Please type the full name of the file.
Goto :FilePrompt
)
:PassPrompt
Echo Please enter the Domain\Admin password.
Set /P DomPass=Domain Admin Password:
If "%DomPass%"=="" (
ECHO Please enter password.
Goto :PassPrompt
)
type %FileInput% > FileFix.reg
type FileFix.reg > %FileInput%
Del /Q FileFix.reg
For /F "delims=" %%i in (%FileInput%) do Call :Online %%i
Goto :EOF
:ONline
set Machine=%~1
set Machine=%Machine: =%
set RESULT=
Echo Computer:%Machine%
CD C:\Scripts\xcacls
If NOT "%Machine%" == "" (
ECHO. >>C:\Scripts\xcacls\LOG_MS
ECHO. >>C:\Scripts\xcacls\LOG_MS
ECHO **************************
ECHO COMPUTER: %Machine% >>C:\Scripts\xcacls\LOG_MS
ECHO **************************
REM ping %Machine% -n 4 -w 100 >Ping_Test.txt
REM findstr /C:"find" Ping_Test.txt
ping -w 1000 %MACHINE% |findstr /r /c:"Reply from" 1>NUL 2>NUL
if errorlevel 1 goto Return_False
if errorlevel 0 goto Return_True
)
:Return_True
set RESULT=Successful Ping
Goto Run_Xcacls
:Return_False
Set RESULT=Failed to Connect
Goto Run_Xcacls
:Run_Xcacls
Echo Result:%Result%
if NOT "%RESULT%" == "Successful Ping" (
ECHO %MAchine% did not respond at %Time% >>C:\Scripts\xcacls\LOG_MS
) Else (
ECHO %Machine% ALIVE! at %Time% >>C:\Scripts\xcacls\LOG_MS
cscript.exe /h:cscript
Xcacls \\%MAchine%\c$\Windows\reg
Xcacls \\%Machine%\c$\Windows\reg
)
:EOF
ASKER
leew got me on the right track... let him get the points.
PING -n 1 192.168.1.1|find "Reply from" >NUL
IF NOT ERRORLEVEL 1 goto :ONLINE
IF ERRORLEVEL 1 goto :OFFLINE
:OFFLINE
ECHO OFFLINE
GOTO END
:ONLINE
ECHO ONLINE
goto END
:END
pause
IF NOT ERRORLEVEL 1 goto :ONLINE
IF ERRORLEVEL 1 goto :OFFLINE
:OFFLINE
ECHO OFFLINE
GOTO END
:ONLINE
ECHO ONLINE
goto END
:END
pause
I got it to work without the "find" (it is not reliable because ping results are different in different languages and on different Windows version):
SETLOCAL ENABLEDELAYEDEXPANSION
set ComputerToInstallOn=SomeCo mputerName
ping !ComputerToInstallOn! -n 1 -w 300 > nul
IF ERRORLEVEL 1 (
ECHO Computer offline
) ELSE (
ECHO Computer online
)
pause
SETLOCAL ENABLEDELAYEDEXPANSION
set ComputerToInstallOn=SomeCo
ping !ComputerToInstallOn! -n 1 -w 300 > nul
IF ERRORLEVEL 1 (
ECHO Computer offline
) ELSE (
ECHO Computer online
)
pause
ASKER
Computer: Comp-XP2
LineCount: 1
Ping request could not find host Comp-XP2. Please check the name and try a
gain.
Error: 0
Press any key to continue . . .
Code:
ping %%i -n 1 -w 100
Echo Error: %errorlevel%
pause