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.
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.
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%
"