A script to continuous ping an address and voice alert when connected

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Cornbeetle
Posts: 31
Joined: 23 Nov 2015 09:34

A script to continuous ping an address and voice alert when connected

#1 Post by Cornbeetle » 30 Sep 2016 11:39

Credit to Aicini for using his idea for voice synthesis in this post: http://www.dostips.com/forum/viewtopic.php?f=3&t=6841&p=44362&hilit=sapi#p44362

Code: Select all

@echo off
:: create random string of numbers to ensure no duplicate file names
set numbers=%random%%random%%random%

:: echo "voice script" commands into batch file
echo @set @a=0 /*>%temp%\speak_%numbers%.bat
echo @CScript //nologo //E:JScript "%%~F0" "%%1 %%2 %%3 %%4 %%5" ^& goto :EOF */>>%temp%\speak_%numbers%.bat
echo WScript.CreateObject("SAPI.SpVoice").Speak(WScript.Arguments(0))>>%temp%\speak_%numbers%.bat

:start
set /p address="Address: "
cls
SET /P X=Pinging "%address%" - Status: <NUL
set oldstate=neither
:loop
set state=down
for /f "tokens=4,7" %%G in ('ping %address% -n 5 ^| find /i "sent"') do (
    if "%%G%%H"=="5,5," set state=up
)

if not %state%==%oldstate% (
    echo.Link is %state%
    set oldstate=%state%
)
if %state%==up goto:linkup
ping -n 2 localholst >nul: 2>nul:
goto :loop

:: Display timestamp and execute "voice script" with variables
:linkup
echo %time% %date%
echo.
call %temp%\speak_%numbers%.bat Connection established with, %address%
call %temp%\speak_%numbers%.bat Repeat. Connection established with, %address%

pause>nul
del %temp%\speak_%numbers%.bat
goto:start

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: A script to continuous ping an address and voice alert when connected

#2 Post by Squashman » 30 Sep 2016 12:51

Is there a question you need answered?

Not quite understanding why you are creating a batch file to execute jscript code?
It would make more sense to do one of two things.

1) Just create a jscript file and execute it from your main batch file.
2) Make your main batch file a hybrid batch/jscript file.

Cornbeetle
Posts: 31
Joined: 23 Nov 2015 09:34

Re: A script to continuous ping an address and voice alert when connected

#3 Post by Cornbeetle » 30 Sep 2016 13:09

Squashman wrote:Is there a question you need answered?

Not quite understanding why you are creating a batch file to execute jscript code?
It would make more sense to do one of two things.

1) Just create a jscript file and execute it from your main batch file.
2) Make your main batch file a hybrid batch/jscript file.


Hybrids have always been my downfall with batch scripting. I usually resort to outputting commands to a file and then having the main script execute that newly create file.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: A script to continuous ping an address and voice alert when connected

#4 Post by Squashman » 30 Sep 2016 13:30

Cornbeetle wrote:Hybrids have always been my downfall with batch scripting. I usually resort to outputting commands to a file and then having the main script execute that newly create file.

That is ironic because the batch file you are creating in your main code is creating a hybrid batch/jscript file.

Cornbeetle
Posts: 31
Joined: 23 Nov 2015 09:34

Re: A script to continuous ping an address and voice alert when connected

#5 Post by Cornbeetle » 30 Sep 2016 13:58

Squashman wrote:
Cornbeetle wrote:Hybrids have always been my downfall with batch scripting. I usually resort to outputting commands to a file and then having the main script execute that newly create file.

That is ironic because the batch file you are creating in your main code is creating a hybrid batch/jscript file.


You are correct [face palm]. What I mean is that I would like to learn to create a hybrid independent (without outputting or using temp files.)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: A script to continuous ping an address and voice alert when connected

#6 Post by foxidrive » 30 Sep 2016 16:05

Just a tip for seaching on DosTips - use google with the dostips domain.

https://www.google.com.au/search?hl=en& ... ostips.com


This might be one of the better threads on the topic:

viewtopic.php?t=5543

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: A script to continuous ping an address and voice alert when connected

#7 Post by aGerman » 30 Sep 2016 16:20

Maybe that explanation helps you to understand how it works:
viewtopic.php?t=7209&p=46984#p46984

I think your script could be changed like that

Code: Select all

@if (@a)==(@b) @end /* Batch part:
@echo off &setlocal EnableDelayedExpansion &set "address="
for /l %%i in () do (
  if not defined address set /p "address=Address: " &cls
  >nul 2>&1 ping -n 1 !address! && (
    echo !time! !date! &echo(
    cscript //nologo //e:jscript "%~fs0" "Connection established with, !address!. Repeat. Connection established with, !address!."
    set "address="
  ) || (echo Ping failed. &>nul timeout /t 1 /nobreak)
)

JScript part: */
WScript.CreateObject('SAPI.SpVoice').Speak(WScript.Arguments(0))

EXIT /B or GOTO :EOF is unnecessary here because the Batch part runs an infinite loop and will never reach the JScript part.

Steffen

Cornbeetle
Posts: 31
Joined: 23 Nov 2015 09:34

Re: A script to continuous ping an address and voice alert when connected

#8 Post by Cornbeetle » 01 Oct 2016 09:53

@aGerman

2 things:
Could you explain how it runs the JScript code and knows where to find it embedded within the batch?
Also, what is happening when there is nothing in the parenthesis in this line
for /l %%i in ()
?

Cornbeetle
Posts: 31
Joined: 23 Nov 2015 09:34

Re: A script to continuous ping an address and voice alert when connected

#9 Post by Cornbeetle » 01 Oct 2016 09:54

foxidrive wrote:Just a tip for seaching on DosTips - use google with the dostips domain.

https://www.google.com.au/search?hl=en& ... ostips.com


This might be one of the better threads on the topic:

viewtopic.php?t=5543


Thank you Foxidrive, this is helpful.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: A script to continuous ping an address and voice alert when connected

#10 Post by aGerman » 01 Oct 2016 10:51

Cornbeetle wrote:Could you explain how it runs the JScript code and knows where to find it embedded within the batch?

My link should have explained it :? You pass the script to cscript.exe in order to execute it as JScript code. Everything between /* and */ is a JScript comment and will be ignored. Only the code after */ will be executed.

Cornbeetle wrote:Also, what is happening when there is nothing in the parenthesis in this line
for /l %%i in ()
?

It's an infinite loop. The code in the body of the loop will be repeatedly executed.

Steffen

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: A script to continuous ping an address and voice alert when connected

#11 Post by Squashman » 01 Oct 2016 16:35

Cornbeetle wrote:Also, what is happening when there is nothing in the parenthesis in this line
for /l %%i in ()
?

Well you could put that code in another batch file and see what it does.

Post Reply