Send Dynamic IP Address to email

Mikrotik script to send dynamic IP Address to email

The method to send dynamic IP Address to email is the ideal solution of providing the latest IP information. Dynamic IP address changes from time to time, so Mikrotik has the perfect solution to this matter.

 

To send dynamic IP Address to email, we need to follow some steps. First of all, we have to create some general configurations: to configure mikrotik mail, to create a system script for sending email with IP Address info etc.

In this case we did an experiment. We have a router in the office which we want to control from home. Let’s say we have two email addresses in Gmail and the router with dynamic IP Address is in your office. First email address is [email protected] and second email is [email protected].

So we want to put the email [email protected] in the router. The router will use this email to send IP address information to the email: [email protected].

How to send dynamic IP Address to email:

How to configure Mikrotik RouterOS to send mail:

  1. Open Winbox and login to the router.
  2. Click Tools then click Email.
  3. In Server: enter ip address  173.194.79.108 .
  4. Enter port  587.
  5. Start TLS, choose Yes.
  6. In the From field enter <[email protected]>.
  7. In User field enter [email protected].
  8. Below enter Gmail password for email: [email protected].
  9. If the email is not sent go to Google account and allow Mikrotik access to email.

 

mikrotik add email

How to change security settings in Google Account to allow the router to use the Gmail:

  1. Go to https://myaccount.google.com/security. On Less secure app access section switch to On, (Use another device to change account security settings).
  2. Open warning email with subject Critical security alert, click Check activity then click Yes, it was me.

I want to be clear: This action makes your account less secure, but it’s the only way it works.

 

Now we need a script that detects Dynamic IP from a specific Interface, and the same script sends the email to: [email protected]

How to create a script on the router to detect and send us IP Address information:

In this case Dynamic IP is located in the interface called Wan. So, let’s create a script like this below.

  1. Click System, click Script.
  2. Enter name: my_dynamic_ip  for new script.
  3. Copy this code and paste in the Script field, click OK.


:local theaddr [/ip address get [find interface="Wan" disabled=no] address]
:local myipaddr [:put [:pick $theaddr 0 [:find $theaddr "/" ] ] ]
:local myintnm [/system identity get name]
:log warning "Current dynamic IP address $myintnm: $myipaddr"
/ip dns static set address=$myipaddr name=router numbers=[find name="router"]
/tool e-mail send to="[email protected]" subject="Router $myintnm - current IP
address: $theaddr" body="Router $myintnm current dynamic IP: $theaddr"

  • Replace "Wan" with your interface name used for internet access.
  • Replace "[email protected]" with your email.

 

Mikrotik detect ip script

 

You can test this config by clicking Run Script. If you receive an email from your router, the config is working fine.

How to create a scheduler to run script everyday at 20 : 00:

  1. Click System then click Scheduler.
  2. Clcik Add.
  3. Enter the correct information in all the fields below.
  4. Name: ip-script-run-daily
  5. Start Date: Jan/01/2018
  6. Start Time:00:00:00
  7. Interval: 1d 00:00:00
  8. On Event, Enter the name of script: my_dynamic_ip
  9. Click OK.

 

Mikrotik Scheduler

You can configure the router to send emails with new IP Address when the internet is interrupted or when your IP address is replaced by your ISP server.

All you need to do is find an IP address that is always active. The router will ping that IP every 5 seconds. At the moment when there is a ping Time Out for about 1000 ms, you will receive the email with the last IP Address.

How to force the router to send us IP Address info each time the IP Address changes:

  1. Click Tool, click Netwatch then click Add.
  2. In Host tab enter the correct information.
  3. Host: Enter an IP Address that is always active.
  4. Interval:00:00:05
  5. Timeout:1000
  6. In UP tab enter the name of IP script: my_dynamic_ip
  7. In Down tab enter: log error message=”Internet disconnected”
  8. Click OK.

 

Mikrotik netwatch log

So it was for this matter. I know, it’s a short text to read but, it’s too long to write.

 

Scroll to Top