Community discussions

MikroTik App
 
himme1500
just joined
Topic Author
Posts: 1
Joined: Tue Dec 04, 2018 7:10 pm

Need help to email ping results / mode button event

Tue Dec 04, 2018 7:24 pm

I apologize for being a nube... can't tell you how many times I have taken a run at learning how to script but often find myself stuck at step1. (I am sure this is very simple to do.)

I would like to have create a script to ping a destination (8.8.8.8) and email results in body of email with hostname, current date and time. I figure that this can be done multiple ways using ping speed, netwatch and such so advice on those is appreciated, but simple ping is fine.

Could someone show me how to do this.
 
User avatar
Steveocee
Forum Guru
Forum Guru
Posts: 1120
Joined: Tue Jul 21, 2015 10:09 pm
Location: UK
Contact:

Re: Need help to email ping results / mode button event

Thu Dec 06, 2018 6:29 pm

You will need to set up /tools email to work correctly but when done use the below to create a script and then run the script on mode button being pressed;
#Define Email variables here
:local toEmail toaddress@mikrotik.com
:local fromEmail fromaddress@mikrotik.com

#Ping Variables
:local avgRtt;

:local pin

:local pout

#Capture system identity for email subject
:local sysID [/system identity get name]

#Capture current time for logging
:local datetime "$[/system clock get date] $[/system clock get time]"


#Ping it real good
/tool flood-ping 8.8.8.8 count=10 do={
  
:if ($sent = 10) do={
    
:set avgRtt $"avg-rtt"
    
:set pout $sent
    
:set pin $received
  }

}

:local ploss (100 - (($pin * 100) / $pout))

:local logmsg ("Ping Average for 8.8.8.8 - ".[:tostr $avgRtt]."ms - packet loss: ".[:tostr $ploss]."%")

:log info $logmsg

:log info message="Emailing Ping Test Results"
delay 2
:tool e-mail send user=$fromEmail to=$toEmail subject="Pingtest results for user $sysID" body="ID: $sysID \n Test Ran at: $datetime \n Result: \n $pingit \n $logmsg"
delay 2
:log info message="Ping Test Results Have Been Emailed"
It will email the chosen address with the below output;

"ID: honeypot
Test Ran at: dec/06/2018 16:25:56
Result:

Ping Average for 8.8.8.8 - 4ms - packet loss: 0%
"

Who is online

Users browsing this forum: No registered users and 27 guests