Computer Hope

Microsoft => Microsoft DOS => Topic started by: Alok_Kumar on February 13, 2007, 11:34:21 PM

Title: Need to create a batchfile, which sends Alerts
Post by: Alok_Kumar on February 13, 2007, 11:34:21 PM


Dear All,

I need to create a batch file, which should first ping to the different i.p address, if it gets request time out from any of them, it should send mail with specific massage. Your swift response wouold be highly appriciated.

hare krishna
Alok
Title: Re: Need to create a batchfile, which sends Alerts
Post by: Dilbert on February 13, 2007, 11:38:56 PM
Good news and bad news.

Good news: It is possible to parse a ping for a timeout. You can do this by outputting the ping results to a text file and trying to find the word "timeout" in the text file.

Bad news: It is impossible to use DOS to send an email. DOS is too old, I'm afraid.
Title: Re: Need to create a batchfile, which sends Alerts
Post by: Alok_Kumar on February 14, 2007, 12:11:24 AM

Thanks Dilbirt,

So any work around of my problem ?

hare krishna
Alok
Title: Re: Need to create a batchfile, which sends Alerts
Post by: ghostdog74 on February 14, 2007, 03:36:13 AM
you can use command line tools for sending emails such as blat (http://www.blat.net/?faq/index.html)

then do something like this assuming only 2 ips. (substitute the blat command as desired)
Code: [Select]
@echo off
set ip=10.10.10.11 10.10.20.2
for /F  "tokens=1* delims= " %%i in ("%ip%") do (
      ping -n 2 %%i > nul
      if errorlevel 1 echo fail
      ping -n 2 %%j > nul
      if errorlevel 1 echo fail
)
Title: Re: Need to create a batchfile, which sends Alerts
Post by: Alok_Kumar on February 14, 2007, 04:36:08 AM


Thank Ghost,

I did get success to send mail through blat, but I am still not able to figure out, how I can use your script in a btchfile, which triggers automatically and send mail back to me
indicating a perticular i.p unable to ping. I've used the given below command to send mail
thanks, once again.


 blat c:\emp.txt -to [email protected] -subject "Test"

hare krishna
Alok
Title: Re: Need to create a batchfile, which sends Alerts
Post by: gpl on February 14, 2007, 05:20:57 AM
If you are running on a modern windows system, you could always use a bit of vbs to send the email and call the vbs from your batch using START....

Graham
Title: Re: Need to create a batchfile, which sends Alerts
Post by: Alok_Kumar on February 14, 2007, 10:19:50 PM


Thanks Gpl,

can't I use, batch programming to achieve this, because, I don't have V.B in my system.
Please advise me, how can implement the above given logic in my last post through batch file programming.

hare krishna
Alok
Title: Re: Need to create a batchfile, which sends Alerts
Post by: Sidewinder on February 15, 2007, 08:19:01 AM
Code: [Select]
@echo off
set ip=10.10.10.11 10.10.20.2
for /F  "tokens=1* delims= " %%i in ("%ip%") do (
      ping -n 2 %%i > nul
      if errorlevel 1 (
              set msg=Ping failed: %%i
              blat c:\emp.txt -to [email protected] -subject %msg%
             )
)
VBScript is installed with all Windows versions except Win95A. The current version is 5.6.

Good luck.  8-)

PS. Sending an email with VBScipt has been posted many times. If you need furthur assistance let us know and yes, you can launch a VBScript from a batch file.

PSS. For your own protection, it  is not a good idea to post email addresses.
Title: Re: Need to create a batchfile, which sends Alerts
Post by: Alok_Kumar on February 19, 2007, 05:10:45 AM


Thanks Winder,

Can you explain in brief of each step in your script. I've tested your script, but it's not working. though, when using only blat, I can send mail. pleas suggest, what causes this script to fail?

hare krishna
Alok
Title: Re: Need to create a batchfile, which sends Alerts
Post by: Sidewinder on February 19, 2007, 05:40:00 AM
Quote
I've tested your script, but it's not working


Please be specific, our resident mystic was run over by a truck. She never saw it coming ;D. Post your console output.

 8-)

Actually I just copied a previous snippet from your thread and added the blat command.
Title: Re: Need to create a batchfile, which sends Alerts
Post by: Alok_Kumar on February 19, 2007, 09:36:49 PM


Once again thanks Winder, I did use the script, which I am pasting below on the board.

@echo off
set ip=110.110.6.5 110.110.6.4
for /F  "tokens=1* delims= " %%i in ("%ip%") do (
      ping -n 2 %%i > nul
      if errorlevel 1 (
              set msg=Ping failed: %%i
             blat c:\emp.txt -to my_email_id -subject %msg%
          )
)

 
I've saved the above script as batch file. When I double click it, command prompt screen comes up and disappers after a while, When I see my mail box, Diden't see any mail.
I need your favour, could your explain me in brief that what is doing this script step by step? thanks, once again.

hare krishna
Alok


 



Title: Re: Need to create a batchfile, which sends Alerts
Post by: Sidewinder on February 20, 2007, 05:13:50 AM
The file setups up a literal of two ip addresses separated by a space (the set statment). A loop is setup and each ip from the set statment is processed (the for statment). Each ip is pinged twice (the ping statment). Next the return code from the ping is checked (the if statment) and if it's 1 or greater (meaning the ping failed) then a message is set up and blat runs (the set and blat statments).

When debugging a file, it helps to run from a command prompt (not double clicking from explorer) and removing echo off will let you see a blow by blow description of what is happening.

For the record, I do not use blat on my machine, but the processor finds no syntax error in the file. For further assistance please post your console output.

 8-)
Title: Re: Need to create a batchfile, which sends Alerts
Post by: Alok_Kumar on February 20, 2007, 10:49:53 PM
Thanks Winder, Your help is really very helpfull to me, though I am not able to execute the last discussed codes, but still able to learn few things from you. I am pasting the code below, which I tried to run at command prompt.

C:\>set ip=132.122.6.5 132.122.6.4

C:\>for /F  "tokens=1* delims= " %%i in ("%ip%") do (
%%i was unexpected at this time.

C:\>50hr8_main.logping -n 2 %%i > nul
'50hr8_main.logping' is not recognized as an internal or external command,
operable program or batch file.

C:\>50hr8_main.logif errorlevel 1 (
'50hr8_main.logif' is not recognized as an internal or external command,
operable program or batch file.

C:\>              set msg=Ping failed: %%i

C:\>             blat c:\emp.txt -to [email protected] -subject %msg%
Blat v2.6.1 w/GSS encryption (build : Nov 18 2006 11:59:59)

Blat saw and processed these options, and was confused by the last one...
    -to [email protected]
    -subject Ping
    failed:
Do not understand argument: failed:

Win32 console utility to send mail via SMTP or post to usenet via NNTP
by P.Mendes,M.Neal,G.Vollant,T.Charron,T.Musson,H.Pesonen,A.Donchey,C.Hyde
  http://www.blat.net
syntax:
  Blat <filename> -to <recipient> [optional switches (see below)]
  Blat -install <server addr> <sender's addr> [<try>[<port>[<profile>]]] [-q]
  Blat -profile [-delete | "<default>"] [profile1] [profileN] [-q]
  Blat -h

hare krishna
Alok
Title: Re: Need to create a batchfile, which sends Alerts
Post by: gpl on February 21, 2007, 03:09:24 AM
I cannot comment about the rest of your code, but the reason the first error came up is due to the way the command processor works .... when in a batch file, %% always resolves to a single %

to run at the command prompt, your command should use single % thus
for /F  "tokens=1* delims= " %i in ("%ip%") do (

and then reference %i later on

This might remove later errors
Graham
Title: Re: Need to create a batchfile, which sends Alerts
Post by: Alok_Kumar on February 21, 2007, 03:25:08 AM

Thanks Gpl, Well I really not able to achive for which I did a post. Well, I would again like to discuss my problem.I just need to creata a batch file, which should ping to some machine and if it gets failed then it should mailed back to me , stating the ping gets failed. If you have any working procedure, then do let me know of those, As I really need to achive the above discussed logic as soon as possible. Thanks, once again.

hare krishna
Alok
Title: Re: Need to create a batchfile, which sends Alerts
Post by: Sidewinder on February 21, 2007, 04:16:49 AM
Quote
I need to create a batch file, which should first ping to the different i.p address, if it gets request time out from any of them, it should send mail with specific massage. Your swift response wouold be highly appriciat

You need to open an editor (notepad is fine), copy/paste the code that has been posted, save the file with a .bat extension, open a command prompt and run the batch file by typing it's name from a command prompt.

The code that has been posted presumes execution from a batch file not by typing code at the command prompt.

 8-)

Note: Be sure to edit the IP addresses posted to valid ones.
Title: Re: Need to create a batchfile, which sends Alerts
Post by: Alok_Kumar on February 21, 2007, 05:24:27 AM

Thanks Winder, I finally able to make it work, but still have one problem, the below given script sending me a mail in eighter of case, regardless of failed or success , but I really like to make it work when any of the set i.p does not ping with the custome massage, Like " Test Link Is Down"

set ip=132.122.6.56 132.122.6.4
for /F  "tokens=1* delims= " %%i in ("%%ip%%") do (
     ping -n 2 %%i > nul
     if errorlevel 1 (
     set msg=Ping failed: %%i
     blat c:\emp.txt -to [email protected] -subject %%AgaraisDown%%
      )
 )
 
how do I modify this script so that it only send me a mail when pining to i.p is failed, else
does not send me a mail.

hare krishna
Alok
Title: Re: Need to create a batchfile, which sends Alerts
Post by: Sidewinder on February 21, 2007, 05:48:16 AM
This should finally end your long nightmare:

Code: [Select]
set ip=132.122.6.56 132.122.6.4
for %%i in ("%ip%") do (
     ping -n 2 %%i > nul
     if errorlevel 1 (
       echo Ping failed: %%i
       blat c:\emp.txt -to [email protected] -subject %%AgaraisDown%%  
     )
 )

Good luck.  8-)
Title: Re: Need to create a batchfile, which sends Alerts
Post by: Alok_Kumar on February 21, 2007, 05:54:31 AM

Thanks Winder, but still I am not out of woods, the above given script is sending a mail, ragardless of ping success, but it should only send me a mail, when any of the set i.p gets request time out, hence failed.

hare krishna
Alok
Title: Re: Need to create a batchfile, which sends Alerts
Post by: Sidewinder on February 21, 2007, 06:01:54 AM
How do you know? The ping results are sent to the null device, so do you really know the results?

Suggestion: remove the > null from the ping statement and see for yourself if the ping fails or succeeds.

Best I can tell, your batch file is logically correct. 8-)

Title: Re: Need to create a batchfile, which sends Alerts
Post by: Alok_Kumar on February 21, 2007, 10:35:20 PM


>> How do you know? The ping results are sent to the null device, so do you really know the results?
 
I did the manual ping of both i.p, both are repling prety well, nighter of those is failing.
I also tried to remove nul, when I run it , it gives back me error.

C:\>"C:\Documents and Settings\alok.kumar\Desktop\test1.bat"

C:\>set ip=132.122.6.5 132.122.6.4
The syntax of the command is incorrect.

C:\>   ping -n 2 %i >

The only problem I am facing here that It continues to send me a mail, reagrdless of ping success. I just don't let this to happen, It should only mail down to me when any of ping gets failed. Thanks Winder for your precious help.

hare krishna
Alok
Title: Re: Need to create a batchfile, which sends Alerts
Post by: Alok_Kumar on February 22, 2007, 03:22:04 AM


Any Inputs ?? This is very urgent!!

hare krishna
Alok
Title: Re: Need to create a batchfile, which sends Alerts
Post by: Sidewinder on February 22, 2007, 03:23:40 AM
I have no idea what's wrong. The batch file as posted returns no errors on my machine. A little late I know, but what OS are you using.

Are you copying/pasting your console output or are you typing it? There seems to be a for statement missing between the set and the ping. Also you need to remove the [highlight]>[/highlight] symbol from the ping for testing purposes.

Not for nothing, a ping of 132.122.6.5 failed miserably.

Quote
Pinging 132.122.6.5 with 32 bytes of data:

Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 132.122.6.5:
    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

I may have used a bad IP address, which seems to change from post to post. 8-)
Title: Re: Need to create a batchfile, which sends Alerts
Post by: Alok_Kumar on February 22, 2007, 04:05:07 AM


I restart once agin, I am using the below script, which is returning no errors, that's fine, but it does not solve the purpose for which I asked the question, Ideally it should not mail me, because both of the i.p defined in the code can be pinged from my system.

set ip=132.122.6.5 132.122.6.4
for %%i in ("%ip%") do (
      ping -n 2 %%i > nul
      if errorlevel 1 (
       echo Ping failed: %%i
       blat c:\emp.txt -to [email protected] -subject "Agara is Down"
      )
 )
 

If I run the above code, by double clicking the batch file, it's sending me a mail stating "Agara is Down" , which is again a nightmare for me. I would say millions
of thanks to you.

hare krishna
Alok
Title: Re: Need to create a batchfile, which sends Alerts
Post by: Sidewinder on February 22, 2007, 04:24:52 AM
As I mentioned, I can't ping 132.122.6.5 successfully. Not sure why you can. Just for kicks replace 132.122.6.5 with 127.0.0.1

127.0.0.1 is the localhost which should always be pingable (is that even a word :-?). I'd be interested in the results.

You still haven't mentioned your OS

 8-)
Title: Re: Need to create a batchfile, which sends Alerts
Post by: Alok_Kumar on February 22, 2007, 05:43:16 AM


Thanks! Winder for the swift responce. My O.S is Winxp SP2. One interesting thing I observed with the above discussed code is that Its sending me a mail on eighter case, case 1 = when ping success
case 2 = when ping does not success.

and I only want this to execute, when ping does not get success. for example, I opened a cmd prompt and ping 127.0.0.1, if it's pingable, then it should not triggre to send a mail.

Now I kept my finger across that you able to understand my requirement.

hare krishna
Alok
Title: Re: Need to create a batchfile, which sends Alerts
Post by: Sidewinder on February 22, 2007, 06:30:02 AM
I understand your requirements. If I use one IP I can ping and another IP I can't ping, these are my results:

Code: [Select]
Pinging 127.0.0.1 with 32 bytes of data:

Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Ping statistics for 127.0.0.1:
    Packets: Sent = 2, Received = 2, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

Pinging 132.122.6.4 with 32 bytes of data:

Request timed out.
Request timed out.

Ping statistics for 132.122.6.4:
    Packets: Sent = 2, Received = 0, Lost = 2 (100% loss),
Ping failed: 132.122.6.4
'blat' is not recognized as an internal or external command,
operable program or batch file.

As you can see, when the ping to 127.0.0.1 succeeded blat did not run. When the ping to
132.122.6.4 failed, blat attempted to run (it failed because blat is not installed on this system...but the attempt was made!)

I'm unsure why you can't duplicate these results. Post some console listings so we can see exactly whats going on in that machine. Just on a wild hunch, make sure the character between the ip addresses on the set statement is really a space.
Title: Re: Need to create a batchfile, which sends Alerts
Post by: Alok_Kumar on February 22, 2007, 09:47:04 PM

Now I am going to past the complete output from the console.

C:\> ping 132.122.6.5

Pinging 132.122.6.5 with 32 bytes of data:

Reply from 132.122.6.5: bytes=32 time=1ms TTL=128
Reply from 132.122.6.5: bytes=32 time=26ms TTL=128
Reply from 132.122.6.5: bytes=32 time=2ms TTL=128
Reply from 132.122.6.5: bytes=32 time=1ms TTL=128

Ping statistics for 132.122.6.5:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 1ms, Maximum = 26ms, Average = 7ms

C:\> ping 132.122.6.4

Pinging 132.122.6.4 with 32 bytes of data:

Reply from 132.122.6.4: bytes=32 time<1ms TTL=128
Reply from 132.122.6.4: bytes=32 time<1ms TTL=128
Reply from 132.122.6.4: bytes=32 time<1ms TTL=128
Reply from 132.122.6.4: bytes=32 time<1ms TTL=128

Ping statistics for 132.122.6.4:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

C:\>"C:\Documents and Settings\alok.kumar\Desktop\test2.bat"

C:\>set ip=132.122.6.5 132.122.6.4

C:\>for %i in ("132.122.6.5 132.122.6.4") do (
ping -n 2 %i  1>nul
 if errorlevel 1 (
echo Ping failed: %i
 blat c:\emp.txt -to [email protected] -subject "Agara is Down"
)
)

C:\>(
ping -n 2 "132.122.6.5 132.122.6.4"  1>nul
 if errorlevel 1 (
echo Ping failed: "132.122.6.5 132.122.6.4"
 blat c:\emp.txt -to [email protected] -subject "Agara is Down"
)
)
Ping failed: "132.122.6.5 132.122.6.4"
Blat v2.6.1 w/GSS encryption (build : Nov 18 2006 11:59:59)

Sending c:\emp.txt to [email protected]
Subject: Agara is Down
Login name is [email protected]

You can very well see that both of the ip is pingable, but blat is running and sending back mail to me, regardless of complete success of both the i.p's.

hare krishna
Alok
Title: Re: Need to create a batchfile, which sends Alerts
Post by: ghostdog74 on February 23, 2007, 01:52:45 AM
you should use tokens so as to get each ip.
Code: [Select]
....
c:\> for /F "tokens=1,2" %i in ("132.122.6.5 132.122.6.4") do echo %i %j
Title: Re: Need to create a batchfile, which sends Alerts
Post by: Sidewinder on February 23, 2007, 03:11:47 AM
The default delimiter of the for statement is a space or a tab. That said, it appears the string in the set statement is not being parsed correctly. I suspect the highlighted character is not a space. Did you check this as my last post requested?

Quote
C:\>(
ping -n 2 "132.122.6.5[highlight] [/highlight]132.122.6.4"  1>nul
 if errorlevel 1 (
echo Ping failed: "132.122.6.5[highlight] [/highlight]132.122.6.4"
 blat c:\emp.txt -to [email protected] -subject "Agara is Down"
)
)
Ping failed: "132.122.6.5 132.122.6.4"
Blat v2.6.1 w/GSS encryption (build : Nov 18 2006 11:59:59)

Suggestion: Delete the set statement and recreate it ensuring the character between the IP addresses is a space and not some character like the right arrow.

You could also put a comma between the ip addresses, override the default delimiters, define the comma as a delimiter, and parse the set string accordingly.
Title: Re: Need to create a batchfile, which sends Alerts
Post by: Alok_Kumar on February 23, 2007, 05:06:11 AM
set[highlight] [/highlight]ip=[highlight] [/highlight]132.122.6.5[highlight] [/highlight]132.122.6.4
for %%i in ("%ip%") do (
      ping -n 2 %%i > nul
      if errorlevel 1 (
       echo Ping failed: %%i
       blat c:\emp.txt -to [email protected] -subject "Agara is Down"
      )
 )

I am again posting my batch file with the highlited area, which actually indicate a space.
I really failed to understand, why it failed to ping, when the i.p's are correct. you just do me your favour, one more time, because , as described by you, I am prety much sure that I've used the space in highlited area.

hare krishna
Alok
Title: Re: Need to create a batchfile, which sends Alerts
Post by: Sidewinder on February 23, 2007, 07:39:10 AM
Try this, I removed the quotes around the %ip% variable. Also there should be no space between the equals and the first digit of the first ip address. The set statement takes everything literally and spaces are valid characters.

Code: [Select]
@echo off
set ip=132.122.6.5 132.122.6.4
for %%i in (%ip%) do (
   ping -n 2 %%i > nul
   if errorlevel 1 (
     echo Ping failed: %%i
     blat c:\emp.txt -to [email protected] -subject "Agara is Down"
    )
 )

Lets start the weekend right and get this thread finished. :D
Title: Re: Need to create a batchfile, which sends Alerts
Post by: Alok_Kumar on February 25, 2007, 11:01:06 PM

Thanks a lot winder, I finally make it work. that's really great! Anyways, I just need one more favour. Can't I use mail_body inside blat, like I used in subject "Agara is down". I just don't want to put the file name, I just wanted to put some custome massage inside blat.

hare krishna
Alok


Title: Re: Need to create a batchfile, which sends Alerts
Post by: Sidewinder on February 26, 2007, 05:11:01 AM
It would be helpful if you posted the working version so everyone could see it. Apparently there is a -body switch where you can put your text.

See: Blat Syntax (http://www.blat.net/syntax/syntax.html)

 8-)
Title: Re: Need to create a batchfile, which sends Alerts
Post by: Alok_Kumar on February 26, 2007, 05:41:21 AM


Thanks Winder, Once again. I managed to make it run with the help of the given link.


hare krishna
Alok