Community discussions

MikroTik App
 
User avatar
nithinkumar2000
Member Candidate
Member Candidate
Topic Author
Posts: 159
Joined: Wed Sep 11, 2019 7:42 am
Location: Coimbatore
Contact:

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

Thu Mar 04, 2021 1:24 pm

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???
 
eldoncito2019
Member
Member
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

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

Thu Mar 04, 2021 4:03 pm

: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;
}
 
User avatar
anav
Forum Guru
Forum Guru
Posts: 19106
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

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

Thu Mar 04, 2021 5:50 pm

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??
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

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

Thu Mar 04, 2021 9:24 pm

: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"
}
 
User avatar
anav
Forum Guru
Forum Guru
Posts: 19106
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

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

Fri Mar 05, 2021 2:58 am

Nice!
(PS sent you an email)
 
eldoncito2019
Member
Member
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

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

Fri Mar 05, 2021 2:49 pm

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.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

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

Fri Mar 05, 2021 3:57 pm

You can send email directly to user when they have added email to their profile.
 
eldoncito2019
Member
Member
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

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

Fri Mar 05, 2021 5:12 pm

You can send email directly to user when they have added email to their profile.


I do not understand anything friend Jotne
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

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

Fri Mar 05, 2021 5:17 pm

Anav did send me a private email to my email asking for something.
It has nothing to do with this post :)
 
User avatar
anav
Forum Guru
Forum Guru
Posts: 19106
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

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

Fri Mar 05, 2021 9:00 pm

geez now everyone will think we are lovers LOL.
 
eldoncito2019
Member
Member
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

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

Fri Mar 05, 2021 10:11 pm

: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.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

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

Fri Mar 05, 2021 10:18 pm

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"
 
User avatar
nithinkumar2000
Member Candidate
Member Candidate
Topic Author
Posts: 159
Joined: Wed Sep 11, 2019 7:42 am
Location: Coimbatore
Contact:

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

Sun Mar 07, 2021 4:35 pm

: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....
 
eldoncito2019
Member
Member
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

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

Tue Mar 09, 2021 6:38 pm

: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.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

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

Tue Mar 09, 2021 6:50 pm

No need to quote the quoted the quoted post :)

Just use the Post Reply button under the post.
 
User avatar
anav
Forum Guru
Forum Guru
Posts: 19106
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

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

Tue Mar 09, 2021 8:12 pm

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!!
 
msatter
Forum Guru
Forum Guru
Posts: 2897
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

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

Tue Mar 09, 2021 8:21 pm

geez now everyone will think we are lovers LOL.
But the emails are exchanging bodily bits since then.....
 
eldoncito2019
Member
Member
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

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

Tue Mar 09, 2021 9:31 pm

Publish your script please, since it doesn't work for me. Thanks.
 
User avatar
anav
Forum Guru
Forum Guru
Posts: 19106
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

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

Tue Mar 09, 2021 10:08 pm

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
 
eldoncito2019
Member
Member
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

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

Tue Mar 09, 2021 11:07 pm

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.
 
changeip
Forum Guru
Forum Guru
Posts: 3830
Joined: Fri May 28, 2004 5:22 pm

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

Wed Mar 10, 2021 1:11 am

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
 
eldoncito2019
Member
Member
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

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

Wed Mar 10, 2021 10:23 pm

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.
 
changeip
Forum Guru
Forum Guru
Posts: 3830
Joined: Fri May 28, 2004 5:22 pm

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

Wed Mar 10, 2021 10:27 pm

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.
 
eldoncito2019
Member
Member
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

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

Wed Mar 10, 2021 10:33 pm

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
 
User avatar
nithinkumar2000
Member Candidate
Member Candidate
Topic Author
Posts: 159
Joined: Wed Sep 11, 2019 7:42 am
Location: Coimbatore
Contact:

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

Sat Mar 13, 2021 1:49 pm

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
 
gotsprings
Forum Guru
Forum Guru
Posts: 2102
Joined: Mon May 14, 2012 9:30 pm

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

Tue Mar 16, 2021 10:48 am

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.
 
tabate47
Long time Member
Long time Member
Posts: 510
Joined: Wed Mar 13, 2013 5:23 am
Location: Los Angeles

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

Sun Apr 17, 2022 5:03 pm

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?
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

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

Tue Apr 19, 2022 5:17 pm

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.
 
tabate47
Long time Member
Long time Member
Posts: 510
Joined: Wed Mar 13, 2013 5:23 am
Location: Los Angeles

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

Tue Apr 19, 2022 8:56 pm

That works, thank you!

Is there any way to add more info to the email like the router name and a message?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

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

Tue Apr 19, 2022 9:01 pm

No, you must use scripts / netwatch
 
tabate47
Long time Member
Long time Member
Posts: 510
Joined: Wed Mar 13, 2013 5:23 am
Location: Los Angeles

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

Tue Apr 19, 2022 9:05 pm

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.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

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

Tue Apr 19, 2022 9:21 pm

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...
 
unique82
just joined
Posts: 15
Joined: Mon Jul 22, 2013 3:59 pm

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

Thu Feb 09, 2023 4:31 pm

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

Who is online

Users browsing this forum: No registered users and 25 guests