Need help from a batch file guru

Athlex

Golden Member
Jun 17, 2000
1,258
2
81
I had an idea for a program that would periodically test all of my monitor-less DC nodes. This would be particularly helpful for one of them because it has a bad power supply and dies after a week or two... :p
Anyway, it's a ping test that would run every half hour or so and I need help with syntax.

ping 192.168.0.149 > pingtest.txt
[insert code that would search for the word "timed" to see if the ping timed out]
net send hurricane 192.168.0.149 has failed (sends an NT popup message to myself, that the node has failed)

Another dumb question, what's the character used to place comments in a batch file? :)
Thanks for the help!

Atx
 

OhioDude

Diamond Member
Apr 23, 2001
4,223
0
0
To mark lines as comments, enter REM as the first three characters of the line followed by a space.

Sorry, can't help you with searching for a string in a file within a batch. :( You can use FIND to search for strings in files but I wouldn't know how to interpret the results from within a batch file. (I could do it for you in a jiffy in Unix.)
 

Athlex

Golden Member
Jun 17, 2000
1,258
2
81
Yeah, one of these days I need to get off my butt and build a unix box. :)
Thx for the reply!
 

Athlex

Golden Member
Jun 17, 2000
1,258
2
81
Would windows 2000 know what to do with a perl script? I've never used perl before
 

SoulAssassin

Diamond Member
Feb 1, 2001
6,135
2
0
NT/2K lets you do error checking on the previous command that completed, so you could do something like:

ping box1
if error == 1 net send username

I haven't had to do this in a while and all the scripts I've written (and my books) are at work so I may be slightly off on the usage but you get the idea. Do a search out there and you'll find it. Keep in mind that this is only going to check to see if the box is responding to network pings which is in no way indicative that SETI is still running or that the machine is happy. Doing a 'net view' or something else that at least checks RPC would be a better indicator but still not a guarantee.
 

Athlex

Golden Member
Jun 17, 2000
1,258
2
81
Thanks for the info, that'll help a lot. I'm not sure if net view would work since the dnet clients I'm planning on pinging are running KLinux.
 

Athlex

Golden Member
Jun 17, 2000
1,258
2
81
Something else I'm interested in doing here is making a log file. How would I go about adding a line to a log every time this program runs? I know there's an "append" program, but help doesn't seem to have any info and there's nothing in a -? parameter...
 

barbary

Senior member
Apr 11, 2000
357
0
71
To add a line to a log just do

echo STARTING PROCESS >> c:\log.txt

For checking if there is an error on one machine the earlier sugestion is best.

Although ping won't work as the command will finish without error even if the machine isn't there.

uptime is good though so something like this

echo off
echo STARTING PROCESS %DATE% %TIME% >> c:\log.txt
uptime %1
if errorlevel 1 (
net send hurricane %1 has failed
)

Put this into a batch called test.bat

the command

test 192.168.0.149
 

barbary

Senior member
Apr 11, 2000
357
0
71
Ahhh they are not NT boxes. Here's a version that works with the ping command and anything on the network.


echo off
set FIRST=
for /F %%n in ('ping %1') DO (
if %%n==Request (
if NOT defined FIRST (
set FIRST=TRUE
net send hurricane %1 has failed
)
)
)
set FIRST=
 

barbary

Senior member
Apr 11, 2000
357
0
71
Uptime command is in the resource kit. I think it is freeware from microsoft now???

I also have a grep command for NT which is freeware. Which makes most of this stuff easier.
 

barbary

Senior member
Apr 11, 2000
357
0
71
Isn't there a way of marking code so that it keeps it's formatting. Think I've asked this before and there isn't.
 

Slahr Dzhe

Senior member
Oct 10, 1999
798
0
0
The REM (remark) command is from those of us who actually used computers in the days of DOS. :p

I don't do much batch command work anymore, so am a bit too rusty to be able to give advice on what would go into the one you are trying to create.

SD


edit: Removed erroneous information. :eek:
 

Athlex

Golden Member
Jun 17, 2000
1,258
2
81
oh yeah. I still remember making various autoexec/config.sys files for different games to eek the most base memory out. Basically ended up with one that would boot with support for my 2x SCSI CD-ROM and the other with those drivers remmed out... :)
 

Athlex

Golden Member
Jun 17, 2000
1,258
2
81
...almost done. Just a couple of more questions :)

What's the difference between ">" and ">>" when piping text into a file? > creates the file and >> appends it?

Also, how can I derive an errorlevel from the ping program? I'm still having a tough time figuring out how to to get a positive or negative result out of it.

Thanks again.
 

Athlex

Golden Member
Jun 17, 2000
1,258
2
81
Okay, I have something that's semi working. Unfortunately it looks like ping returns error level "0" if the ping is successful OR if it times out. It returns error code 1 if it can't find the host...
Any ideas? Here's what it looks like:

@echo off
title Ping test for dc clients
echo.
echo Starting ping test of %1 - %DATE% %TIME%
echo %DATE% %TIME% - pinging %1 >> c:\pingtestlog.txt
goto test

:test
ping %1 -n 1
if errorlevel 1 (goto error1) else (goto success)

:error1
rem Errorlevel 1 = unknown host
echo.
echo Ping %1 failed.
echo %DATE% %TIME% - %1 failed. >> c:\pingtestlog.txt
net send hurricane Ping %1 failed
goto endprog

:success
rem Errorlevel 0 = ping okay, or ping timed out
echo.
echo Got response from %1, looks like everything is okay.
echo Success pinging %1. >> c:\pingtestlog.txt
goto endprog

:endprog
echo. >> c:\pingtestlog.txt
 

barbary

Senior member
Apr 11, 2000
357
0
71
Yes you can't use ping in this way with errorlevel. Thats why I posted the example using ping above.