Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Need to create a batchfile, which sends Alerts  (Read 29087 times)

0 Members and 1 Guest are viewing this topic.

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: Need to create a batchfile, which sends Alerts
« Reply #15 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.
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

Alok_Kumar

  • Guest
Re: Need to create a batchfile, which sends Alerts
« Reply #16 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
« Last Edit: February 21, 2007, 05:28:38 AM by Alok_Kumar »

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: Need to create a batchfile, which sends Alerts
« Reply #17 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-)
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

Alok_Kumar

  • Guest
Re: Need to create a batchfile, which sends Alerts
« Reply #18 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

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: Need to create a batchfile, which sends Alerts
« Reply #19 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-)

« Last Edit: February 21, 2007, 06:03:12 AM by Sidewinder »
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

Alok_Kumar

  • Guest
Re: Need to create a batchfile, which sends Alerts
« Reply #20 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

Alok_Kumar

  • Guest
Re: Need to create a batchfile, which sends Alerts
« Reply #21 on: February 22, 2007, 03:22:04 AM »


Any Inputs ?? This is very urgent!!

hare krishna
Alok

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: Need to create a batchfile, which sends Alerts
« Reply #22 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-)
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

Alok_Kumar

  • Guest
Re: Need to create a batchfile, which sends Alerts
« Reply #23 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

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: Need to create a batchfile, which sends Alerts
« Reply #24 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-)
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

Alok_Kumar

  • Guest
Re: Need to create a batchfile, which sends Alerts
« Reply #25 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

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: Need to create a batchfile, which sends Alerts
« Reply #26 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.
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

Alok_Kumar

  • Guest
Re: Need to create a batchfile, which sends Alerts
« Reply #27 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

ghostdog74



    Specialist

    Thanked: 27
    Re: Need to create a batchfile, which sends Alerts
    « Reply #28 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

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Need to create a batchfile, which sends Alerts
    « Reply #29 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.
    « Last Edit: February 23, 2007, 03:16:58 AM by Sidewinder »
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein