Page 1 of 1

Email Script When Interface Status Change (Running or Not Running)

Posted: Thu Mar 04, 2021 1:24 pm
by nithinkumar2000
Hello,

We are trying to code a script which will check/monitor the Interface Running status and when ever the interface status is change from Running-->not running or vice versa then it will send an Email Alert to Admin.

Can anyone Help us???

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Thu Mar 04, 2021 4:03 pm
by eldoncito2019
:global currentIP;
:local newIP [/ip address get [find interface="ethert1"] address];
:if ($newIP != $currentIP) do={
    :local newIP [/ip address get [find interface="ethert1"] address];
/tool e-mail send to="xxxxx@hotmail.com" subject=([/system identity 
get name]) body=" This is your new IP:  $newIP" start-tls=yes;
    :set currentIP $newIP;
}

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Thu Mar 04, 2021 5:50 pm
by anav
Can we assume that the script can be applied to any interface on the router (aka its not specifically designed of the WAN interface)?
How is the script scheduled then??

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Thu Mar 04, 2021 9:24 pm
by Jotne
:global currentIP;
...
This script tests the for change of IP, not if interface is running or not that Op requested.


This will test status of interface ether1. If status changes, it will run the line :put "change to xxxx", that can be change to email a message.
:global ifstatus
:if ([/interface ethernet get ether1 running]=true) do={
	:put "running"
	:if ($ifstatus="down") do={
		:put "changed to up"
	}
	:set $ifstatus "up"
} else={
	:put "not running"
	:if ($ifstatus="up") do={
		:put "changed to down"
	}
	:set $ifstatus "down"
}

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Fri Mar 05, 2021 2:58 am
by anav
Nice!
(PS sent you an email)

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Fri Mar 05, 2021 2:49 pm
by eldoncito2019
Nice!
(PS sent you an email)
Where did you put the email, to send it to me it doesn't work for me, put your script please.

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Fri Mar 05, 2021 3:57 pm
by Jotne
You can send email directly to user when they have added email to their profile.

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Fri Mar 05, 2021 5:12 pm
by eldoncito2019
You can send email directly to user when they have added email to their profile.


I do not understand anything friend Jotne

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Fri Mar 05, 2021 5:17 pm
by Jotne
Anav did send me a private email to my email asking for something.
It has nothing to do with this post :)

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Fri Mar 05, 2021 9:00 pm
by anav
geez now everyone will think we are lovers LOL.

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Fri Mar 05, 2021 10:11 pm
by eldoncito2019
:global currentIP;
...
This script tests the for change of IP, not if interface is running or not that Op requested.


This will test status of interface ether1. If status changes, it will run the line :put "change to xxxx", that can be change to email a message.
:global ifstatus
:if ([/interface ethernet get ether1 running]=true) do={
	:put "running"
	:if ($ifstatus="down") do={
		:put "changed to up"
	}
	:set $ifstatus "up"
} else={
	:put "not running"
	:if ($ifstatus="up") do={
		:put "changed to down"
	}
	:set $ifstatus "down"
}

Jotne, in which part of the script should I put my email so that when the interface changes, please let me know.

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Fri Mar 05, 2021 10:18 pm
by Jotne
Its written in the post.
it will run the line :put "change to xxxx", that can be change to email a message.
Replace with some like these two lines
/tool e-mail send to=youremail@gmail.com subject="Host is D own" from=youremail@gmail.com port=587 start-tls=yes user=youremail@gmail.com password=123456 body="IF Down"
/tool e-mail send to=youremail@gmail.com subject="Host is D own" from=youremail@gmail.com port=587 start-tls=yes user=youremail@gmail.com password=123456 body="IF UP"

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Sun Mar 07, 2021 4:35 pm
by nithinkumar2000
:global currentIP;
...
This script tests the for change of IP, not if interface is running or not that Op requested.


This will test status of interface ether1. If status changes, it will run the line :put "change to xxxx", that can be change to email a message.
:global ifstatus
:if ([/interface ethernet get ether1 running]=true) do={
	:put "running"
	:if ($ifstatus="down") do={
		:put "changed to up"
	}
	:set $ifstatus "up"
} else={
	:put "not running"
	:if ($ifstatus="up") do={
		:put "changed to down"
	}
	:set $ifstatus "down"
}
Hi Jotne,

Thank You so much for your Help!!!

Script Worked for me....

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Tue Mar 09, 2021 6:38 pm
by eldoncito2019
:global currentIP;
...
This script tests the for change of IP, not if interface is running or not that Op requested.


This will test status of interface ether1. If status changes, it will run the line :put "change to xxxx", that can be change to email a message.
:global ifstatus
:if ([/interface ethernet get ether1 running]=true) do={
	:put "running"
	:if ($ifstatus="down") do={
		:put "changed to up"
	}
	:set $ifstatus "up"
} else={
	:put "not running"
	:if ($ifstatus="up") do={
		:put "changed to down"
	}
	:set $ifstatus "down"
}
Hi Jotne,

Thank You so much for your Help!!!

Script Worked for me....

Greetings, friend could you share your script, since it does not work for me. Thanks.

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Tue Mar 09, 2021 6:50 pm
by Jotne
No need to quote the quoted the quoted post :)

Just use the Post Reply button under the post.

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Tue Mar 09, 2021 8:12 pm
by anav
The email part of the script worded for me once i removed the - start tls - portion. Since tls is already set on my tools - emails settings!!

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Tue Mar 09, 2021 8:21 pm
by msatter
geez now everyone will think we are lovers LOL.
But the emails are exchanging bodily bits since then.....

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Tue Mar 09, 2021 9:31 pm
by eldoncito2019
Publish your script please, since it doesn't work for me. Thanks.

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Tue Mar 09, 2021 10:08 pm
by anav
Sure but my script has a different purpose.
I use recursive routing and whenever the gateway changes on my ISP, the gatewayIPs, in my IP routes do not get updated.
This script addresses that.

:if ($bound=1) do={
:local iface $interface
:local gw [ /ip dhcp-client get [ find interface=$"iface" ] gateway ]
/ip route set [ find comment="PrimaryRecursive" gateway!=$gw ] gateway=$gw
/ip route set [ find comment="SecondaryRecursive" gateway!=$gw ] gateway=$gw
/tool e-mail send to="myaddress@email.com" subject=([/system identity
get name]) body=" This is your new gateway IP: $newIgw";
}

My TOOLS EMAIL Setting.
/tool e-mail
set address=ISP_email_server_IP from=myaddress@email.com port=465 start-tls=\
tls-only user=myaddress@email.com

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Tue Mar 09, 2021 11:07 pm
by eldoncito2019
Hello,

We are trying to code a script which will check/monitor the Interface Running status and when ever the interface status is change from Running-->not running or vice versa then it will send an Email Alert to Admin.

Can anyone Help us???
Share your script please.

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Wed Mar 10, 2021 1:11 am
by changeip
Just let the router do it internally - instead of a script...

/system logging
add action=emailalert disabled=no prefix="" topics=info,interface

Then change your email alert settings:

/system logging action
remove [find name="emailalert"]
add email-to=noc@youremaildomain.com name=emailalert target=email

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Wed Mar 10, 2021 10:23 pm
by eldoncito2019
Just let the router do it internally - instead of a script...

/system logging
add action=emailalert disabled=no prefix="" topics=info,interface

Then change your email alert settings:

/system logging action
remove [find name="emailalert"]
add email-to=noc@youremaildomain.com name=emailalert target=email

I do not understand dear friend.

Re: Email Script When Interface Status Change (Running or Not Running)  [SOLVED]

Posted: Wed Mar 10, 2021 10:27 pm
by changeip
The built in syslog functions will put into the log when an interface goes up and down. YOu just need to add a syslog event to email that to you. We use it all the time - every time an interface goes up and down it will email our NOC. No script needed. Try it. Paste in those commands with a valid working email relay and you should get emails.

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Wed Mar 10, 2021 10:33 pm
by eldoncito2019
The built in syslog functions will put into the log when an interface goes up and down. YOu just need to add a syslog event to email that to you. We use it all the time - every time an interface goes up and down it will email our NOC. No script needed. Try it. Paste in those commands with a valid working email relay and you should get emails.
I just pasted the commands and it doesn't create any rules, my email is enkas012016@gmai.com

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Sat Mar 13, 2021 1:49 pm
by nithinkumar2000
The built in syslog functions will put into the log when an interface goes up and down. YOu just need to add a syslog event to email that to you. We use it all the time - every time an interface goes up and down it will email our NOC. No script needed. Try it. Paste in those commands with a valid working email relay and you should get emails.
Solution is simple and Easy rather then Scripting....

Thank You So much for the Idea

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Tue Mar 16, 2021 10:48 am
by gotsprings
Sure but my script has a different purpose.
I use recursive routing and whenever the gateway changes on my ISP, the gatewayIPs, in my IP routes do not get updated.
This script addresses that.

:if ($bound=1) do={
:local iface $interface
:local gw [ /ip dhcp-client get [ find interface=$"iface" ] gateway ]
/ip route set [ find comment="PrimaryRecursive" gateway!=$gw ] gateway=$gw
/ip route set [ find comment="SecondaryRecursive" gateway!=$gw ] gateway=$gw
/tool e-mail send to="myaddress@email.com" subject=([/system identity
get name]) body=" This is your new gateway IP: $newIgw";
}

My TOOLS EMAIL Setting.
/tool e-mail
set address=ISP_email_server_IP from=myaddress@email.com port=465 start-tls=\
tls-only user=myaddress@email.com
When my DHCP clients update... A script in the DHCP client updates my recursive gateways.

So it happens at every DHCP lease.

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Sun Apr 17, 2022 5:03 pm
by tabate47
Just let the router do it internally - instead of a script...

/system logging
add action=emailalert disabled=no prefix="" topics=info,interface

Then change your email alert settings:

/system logging action
remove [find name="emailalert"]
add email-to=noc@youremaildomain.com name=emailalert target=email
I tried this and nothing happened. I went into system logging and tried to do it manually but there is no email option.

Do you need to be on a certain routeros for this to work?

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Tue Apr 19, 2022 5:17 pm
by diamuxin
The built in syslog functions will put into the log when an interface goes up and down. YOu just need to add a syslog event to email that to you. We use it all the time - every time an interface goes up and down it will email our NOC. No script needed. Try it. Paste in those commands with a valid working email relay and you should get emails.
Great idea! thanks.
/system logging action
add email-start-tls=yes email-to=myemail@gmail.com name=sendinterface target=email

/system logging
add action=sendinterface topics=interface
BR.

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Tue Apr 19, 2022 8:56 pm
by tabate47
That works, thank you!

Is there any way to add more info to the email like the router name and a message?

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Tue Apr 19, 2022 9:01 pm
by rextended
No, you must use scripts / netwatch

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Tue Apr 19, 2022 9:05 pm
by tabate47
Thanks Rex.

What is the best way to send an email upon failover? I'm using recursive routes but I don't have any scripting set up. I was trying to avoid scripts if possible, but it seems like for this I will need it.

I just want to email when the main isp goes down, and then again when it comes back up.

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Tue Apr 19, 2022 9:21 pm
by rextended
First of all, check other topics about dual wan failover, then simply netwatch 1.1.1.1 and 8.8.8.8 and send proper mail accordingly...

Re: Email Script When Interface Status Change (Running or Not Running)

Posted: Thu Feb 09, 2023 4:31 pm
by unique82
Just let the router do it internally - instead of a script...

/system logging
add action=emailalert disabled=no prefix="" topics=info,interface

Then change your email alert settings:

/system logging action
remove [find name="emailalert"]
add email-to=noc@youremaildomain.com name=emailalert target=email
This does work, but for the love of god i can't make mikrotik send any information about the board thus i don't know what is it good for. There is a field "from" : Name or email address that will be shown as receiver.
But it doesnt work i tried to put device name there or an IP but i wont recieve an email, it does show in a log that has been sent but never deleivered. You have to change it to exactly same name as "user" is then it works. Then i wont recognize from which device it was sent, am i missing something? using ROS 6.48 on this particular board.

https://wiki.mikrotik.com/wiki/Manual:T ... Properties