send email if ping breaks to particular IP

I would like a dos batch script to ping an IP at certain time intervals (seconds ideally) and email me if the pings timeout.

I need this to ping the multiple servers , so I will receive an email when any of the server is down or disconnects.
Hiren LapsiwalaManager - Information TechnologyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NumbidCommented:
Basically, you need supervision ;) Have you ever heard about Nagios-like tools ? Many smaller tools can do the job too...

Sending a mail every second if ping times out doesn't seem to be a good idea...

To answer your question, you can build a kind of ping "return code" :

set ip=XXX.XXX.XXX.XXX
ping -n 1 %ip% | find "TTL"
if not errorlevel 1 set result=OK
if errorlevel 1 set result=FAIL
cls
echo Ping result : %result%

Then use a scriptable mail client to send mail depending on the above result.
0
Hiren LapsiwalaManager - Information TechnologyAuthor Commented:
Can you give me the full script to run. the email will not be sent every second but can be configured in task scheduler to run every 30 mins and if ping breaks for 10 times, it will send an email.
0
NumbidCommented:
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

marek1712Commented:
Free solution - PowerShell script:
$counter = 0
$computer_name = "YOUR_HOST"

$emailSmtpServer = "mail.somewhere.com"
$emailSmtpServerPort = "587"
$emailSmtpUser = "username"
$emailSmtpPass = "password"
 
$emailFrom = "John Smith <john@somewhere.com>"
$emailTo = "jane@somewhere.com"
 
$emailMessage = New-Object System.Net.Mail.MailMessage( $emailFrom , $emailTo )
$emailMessage.Subject = "Connection timeout with: " + $computer_name
$emailMessage.IsBodyHtml = $false

$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort )
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass );

while(1)
{
    if ((Test-Connection -ComputerName $computer_name -Count 1 -Quiet) -eq $false)
    {
        $counter++; 
        #Write-Host $counter
    }
    if ($counter -eq 5)
    {
        #Write-Host "Timeout at: " (Get-Date).ToString()
        $emailMessage.Body = "Timeout at: "+(Get-Date).ToString();
        $SMTPClient.Send( $emailMessage );
        $counter = 0;
    }
}

Open in new window

I'm too lazy to write down SMTP client - taken from HERE.

Little advice - this script is an unending loop. Change the while loop in order to make it finite.
0
Hiren LapsiwalaManager - Information TechnologyAuthor Commented:
Thanks sir it is working fine but i need a change in script to check the ping 5 times. If it fails for 5 times then it sends email and the script closes. Email will go only once and script will run through scheduler.

Can you give me this changed script.
0
marek1712Commented:
Hello.
It'd be something like this then:
$counter = 0
$computer_name = "YOUR_HOST"

$emailSmtpServer = "mail.somewhere.com"
$emailSmtpServerPort = "587"
$emailSmtpUser = "username"
$emailSmtpPass = "password"
 
$emailFrom = "John Smith <john@somewhere.com>"
$emailTo = "jane@somewhere.com"
 
$emailMessage = New-Object System.Net.Mail.MailMessage( $emailFrom , $emailTo )
$emailMessage.Subject = "Connection timeout with: " + $computer_name
$emailMessage.IsBodyHtml = $false

$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort )
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass );

for ($i = 0; $i -lt 5; $i++)
{
    if ((Test-Connection -ComputerName $computer_name -Count 1 -Quiet) -eq $false)
    {
        $counter++; 
        #Write-Host $counter
    }
    if ($counter -eq 5)
    {
        #Write-Host "Timeout at: " (Get-Date).ToString()
        $emailMessage.Body = "Timeout at: "+(Get-Date).ToString();
        $SMTPClient.Send( $emailMessage );
        #$counter = 0;
    }
}

Open in new window

The script goes through the loop 5 times. In each loop it counts timeouts. If timeout counter reaches 5 - it sends an e-mail. You can schedule the script to run like every 5 minutes. Is it okay?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Hiren LapsiwalaManager - Information TechnologyAuthor Commented:
Thanks Sir.. Its working perfect..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Legacy OS

From novice to tech pro — start learning today.