Page 1 of 2 12>
Topic Options
#57853 - 2001-08-02 03:50 PM automatic emails if ping fails
Peter Fry Offline
Getting the hang of it

Registered: 2001-07-23
Posts: 95
Loc: Bristol UK
ok i want to set up a batch file that runs via the AT command in NT that pings an IP address and if the outcome of the is an error emails the admin team

i can get the email to work by using
mapisend.exe from the exchange resource kit

i am having problems tho with getting the response from 'ping'

please help


Pete

Top
#57854 - 2001-08-02 04:05 PM Re: automatic emails if ping fails
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Peter,

Here's one way to do it. There has been much debate on what the find string should be for this technique ... anyone ? (j?):

code:

break on


$pings = 2
$machine = "shawn"


shell '%comspec% /c ping -n $pings $machine | find /i "received = $pings" >nul 2>nul'


if @error=0
?"$machine is alive"
else
?"$machine is dead or in trouble"
endif


exit


-Shawn

[ 02 August 2001: Message edited by: Shawn ]

Top
#57855 - 2001-08-02 04:19 PM Re: automatic emails if ping fails
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
S.,

it was "TTL="

J.


P.: see Ping()

[ 02 August 2001: Message edited by: jpols ]

_________________________



Top
#57856 - 2001-08-02 04:30 PM Re: automatic emails if ping fails
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
well.
I use
shell '%comspec% /c ping $ip -n 1 -w 20|find /c "(0% loss),'+'" > %temp%\invis.tmp'

in my inventory script. -w makes it not to wait for many seconds. 0% loss is string that is included in ping reply no matter what language is installed.
sadness is that it needs to be readen from file. and I don't know howto pipe the errorlevel with anything.

_________________________
!

download KiXnet

Top
#57857 - 2001-08-02 04:31 PM Re: automatic emails if ping fails
Peter Fry Offline
Getting the hang of it

Registered: 2001-07-23
Posts: 95
Loc: Bristol UK
anyway to do this using normal batch scipts? and not kixtart?

just so i can see how it would be done?

Top
#57858 - 2001-08-02 04:39 PM Re: automatic emails if ping fails
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
ok.
I checked this and variable %errorlevel% is set to 1 if find does not find the string.

in normal batch file it would be:
%comspec% /c ping 292.117.139.29 -n 1 -w 20|find /c "(0% loss)"
if "%errorlevel%"=="1" do something...

and in kix then:
shell '%comspec% /c ping 292.117.139.29 -n 1 -w 20|find /c "(0%'+' loss)"'
if @error<>1 do something...

try it and say if it works (on my machine it worked fine...)

cheers.

btw. jochen, try this method to your ping() if you get it work, execution time dries smaller like beer in the sun

[ 02 August 2001: Message edited by: Lonkero ]

_________________________
!

download KiXnet

Top
#57859 - 2001-08-02 04:49 PM Re: automatic emails if ping fails
Peter Fry Offline
Getting the hang of it

Registered: 2001-07-23
Posts: 95
Loc: Bristol UK
%comspec% /c ping sh1262 -n 1 -w 20|find /c "(0% loss)"
if "%errorlevel%"=="1" do mapisend -u test -p s -r pete.fry -s "ping failed"


D:\test-dir>mailto

D:\test-dir>C:\WINNT\system32\cmd.exe /c ping sh1262 -n 1 -w 20 | find /c "(0 loss)"
0

D:\test-dir>if "1" == "1" do mapisend -u test -p s -r pete.fry -s "ping failed"

The name specified is not recognized as an
internal or external command, operable program or batch file.


this is what i get when i run the batch file....... what am i doing wrong?

i'm trying to use the without kix approach atm

Pete

Top
#57860 - 2001-08-02 04:54 PM Re: automatic emails if ping fails
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
heh, silly me. I said it using confusing style.
the command do is not recognized.
try in batch:
%comspec% /c ping sh1262 -n 1 -w 20|find /c "(0% loss)"
if "%errorlevel%"=="1" mapisend -u test -p s -r pete.fry -s "ping failed"
and/or:
%comspec% /c ping sh1262 -n 1 -w 20|find /c "(0%% loss)"
if "%errorlevel%"=="1" mapisend -u test -p s -r pete.fry -s "ping failed"
_________________________
!

download KiXnet

Top
#57861 - 2001-08-02 04:58 PM Re: automatic emails if ping fails
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Peter,

The % sign is screwing up the batch file - try it with double %'s eg:

%comspec% /c ping 292.117.139.29 -n 1 -w 20|find /c "(0%% loss)"

-Shawn

Lonkero - the quickest gun in the west.

[ 02 August 2001: Message edited by: Shawn ]

Top
#57862 - 2001-08-02 05:09 PM Re: automatic emails if ping fails
Peter Fry Offline
Getting the hang of it

Registered: 2001-07-23
Posts: 95
Loc: Bristol UK
ok here are my results

%comspec% /c ping sh1263 -n 1 -w 20|find /c "(0%% loss)"
%errorlevel%

machine sh1263 doesn't exist and doing a ping sh1263 normally gets requests timed out

results in an errorlevel of 1 (which is what i wanted)

%comspec% /c ping sh1262 -n 1 -w 20|find /c "(0%% loss)"
%errorlevel%

machine sh1262 exists but i sell get an errorlevel 1

any ideas?

Pete

Top
#57863 - 2001-08-02 05:16 PM Re: automatic emails if ping fails
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Not so fast Yakamoto

what is '0% loss' ???

Never seen that ! is it 9x ?????
In NT a ping reply is for example:

Reply from 10.131.96.20: bytes=32 time=30ms TTL=124

i use this in 3.63 (no temp files needed):

code:

$machine = "whatever"
$timeout = 5000
$pingloop = 6
gosub ping
if $reply
; continue
else
; loop next
endif
.
.
.
ing
$reply = 0
$c = 0
do
shell '%ComSpec /c /e:1024 ping ' + $machine + ' -n 1 -w ' + $timeout + ' | find /C "TTL=" > nul'
if @error = 0
$reply = 1
return
endif
$c = $c + 1
until $c = $pingloop
return


J.

[ 02 August 2001: Message edited by: jpols ]

_________________________



Top
#57864 - 2001-08-02 05:18 PM Re: automatic emails if ping fails
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
My idea is that there is some other failure.
I did it like this:

code:

%comspec% /c ping pikvalexchange -n 1 -w 20|find /c "(0% loss)"
echo "%errorlevel%"
%comspec% /c ping pikvalexchange -n 1 -w 20|find /c "(0%% loss)"
echo "%errorlevel%"


pikvalexchange = e-mail server
and the result was:
code:

C:\kix2k1\rc1>test.bat

C:\kix2k1\rc1>C:\WINNT\system32\cmd.exe /c ping pikvalexchange -n 1 -w 20 | find /c
0

C:\kix2k1\rc1>echo "1"
"1"

C:\kix2k1\rc1>C:\WINNT\system32\cmd.exe /c ping pikvalexchange -n 1 -w 20 | find /c
1

C:\kix2k1\rc1>echo "0"
"0"


so it worked.
take the "-w 20" of and test again. it means that ping does not wait the reply longer than 20ms before quitting with error.
if you have slower net you might try to take it of or set to some 100ms.

cheers,

_________________________
!

download KiXnet

Top
#57865 - 2001-08-02 05:20 PM Re: automatic emails if ping fails
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
... and again : what is your os ???

I never seen 0%loss except maybe in a telnet session on a Router !!!


Jochen

_________________________



Top
#57866 - 2001-08-02 05:23 PM Re: automatic emails if ping fails
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
hey jay pols!
well, this 0% loss is definitely NOT win9x. but you are right, it can be for some specific producttype examp win2k.
my ping gives me (on my win2k wksta):
code:

C:\kix2k1\rc1>C:\WINNT\system32\cmd.exe /c ping pikval_exc -n 1 -w 20

Ping-isäntä pikval_exc.pikval.gws.fi [194.197.109.209], 32 tavun paketti:

Vastaus isännältä 194.197.109.209: tavuja=32 aika<10 ms TTL=128

Ping-tilastot 194.197.109.209:
Paketit: Lähetetty = 1, Vastaanotettu = 1, Kadonnut = 0 (0% loss),
Arvioitu kiertoaika millisekunneissa:
Minimi = 0 ms, Maksimi = 0 ms, Keskimäärin = 0 ms


so, I thought that loss as only english word might be nice thing to search for.

[ 02 August 2001: Message edited by: Lonkero ]

_________________________
!

download KiXnet

Top
#57867 - 2001-08-02 05:30 PM Re: automatic emails if ping fails
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Yeeehaw !

so 'TTL=' is the one and only string !!!

Don't try to search for 'reply from' that would give @error = 0 also for the following:

Reply from RouterIP TTL expired in transit


J.

_________________________



Top
#57868 - 2001-08-02 05:32 PM Re: automatic emails if ping fails
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
like I said - much debate
Top
#57869 - 2001-08-02 05:35 PM Re: automatic emails if ping fails
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Hey Shawn,

a true word composedly spoken ...

Happy 1500th Post Mate !!!


Jochen

_________________________



Top
#57870 - 2001-08-02 05:36 PM Re: automatic emails if ping fails
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
jpols,
you got me think about it and checked on nt machine, sorry...

you are right, there is not (0% loss) on nt.

_________________________
!

download KiXnet

Top
#57871 - 2001-08-02 05:50 PM Re: automatic emails if ping fails
Peter Fry Offline
Getting the hang of it

Registered: 2001-07-23
Posts: 95
Loc: Bristol UK
ok so what should i be doing?
using NT and trying to get this working with out kixtart

how do i get the error levels to work?

Pete

Top
#57872 - 2001-08-02 06:00 PM Re: automatic emails if ping fails
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Pete,

what ? you don't want to use kix ????
Get off !
No, just kidding ...

sample batch I (once) used (called by some other batches (%1 = Computername)

code:

@echo off
if "%logfile%."=="." SET logfile=CON
rem %1: Servername
echo .............................
echo Workin' @ %1
SET PINGLOOP=0
:STARTPING
if %PINGLOOP% == 9 echo %1: No ping reply >> %logfile%
if %PINGLOOP% == 9 GOTO SENDMAIL
SET /a PINGLOOP += 1
ping %1 -n 5 -w 5000 | find /C "TTL=" > nul
if errorlevel 1 GOTO STARTPING
exit
:SENDMAIL
Your mail command here !


hth

Jochen


ps : errrmmm, this was used in a VWAN (Very Wide Area Network) so you might wanna ammend timeout, retries, and loops

[ 02 August 2001: Message edited by: jpols ]


Uups , and not to fergot the calling batch :

code:

@echo off
set logfile=%0\..\log\miss.log
CALL %0\..\Yourbatchname.CMD Computername1
CALL %0\..\Yourbatchname.CMD Computername2
CALL %0\..\Yourbatchname.CMD Computername3
.
.

[ 02 August 2001: Message edited by: jpols ]

_________________________



Top
Page 1 of 2 12>


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 363 anonymous users online.
Newest Members
Sir_Barrington, batdk82, StuTheCoder, M_Moore, BeeEm
17886 Registered Users

Generated in 0.087 seconds in which 0.024 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org