send current IP via mail / mail sending script

Hi guys,

I have a bit of a problem I cannot solve:

I configrued and testet /tools email via Winbox.
When I want to send an email with the Send Email Function everything goes well so I believe my smtp config is ok.

Here is the log of a succesful mail:

Jul/21/2022 22:08:57 e-mail,debug recv: 220 gmx.xxx (mrgmx004) Nemesis ESMTP Service ready
Jul/21/2022 22:08:57 e-mail,debug send EHLO [84.112.XXX.XXX]
Jul/21/2022 22:08:58 e-mail,debug recv: 250-gmx.net Hello [84.112.XXX.XXX [84.112.XXX.XXX]
Jul/21/2022 22:08:58 e-mail,debug recv: 250-8BITMIME
Jul/21/2022 22:08:58 e-mail,debug recv: 250-SIZE 69920427
Jul/21/2022 22:08:58 e-mail,debug recv: 250 STARTTLS
Jul/21/2022 22:08:58 e-mail,debug send STARTTLS
Jul/21/2022 22:08:58 e-mail,debug recv: 220 OK
Jul/21/2022 22:08:59 e-mail,debug send EHLO [84.112.XXX.XXX]
Jul/21/2022 22:08:59 e-mail,debug recv: 250-gmx.xxx Hello [84.112.XXX.XXX] [84.112.XXX.XXX]
Jul/21/2022 22:08:59 e-mail,debug recv: 250-8BITMIME
Jul/21/2022 22:08:59 e-mail,debug recv: 250-AUTH LOGIN PLAIN
Jul/21/2022 22:08:59 e-mail,debug recv: 250 SIZE 69920427
Jul/21/2022 22:08:59 e-mail,debug send AUTH PLAIN
Jul/21/2022 22:09:00 e-mail,debug recv: 235 Authentication succeeded
Jul/21/2022 22:09:00 e-mail,debug send MAIL FROM: <sender@gmx.xxx>
Jul/21/2022 22:09:00 e-mail,debug recv: 250 Requested mail action okay, completed
Jul/21/2022 22:09:00 e-mail,debug send RCPT TO: <recipient@gmx.xxx>
Jul/21/2022 22:09:00 e-mail,debug recv: 250 OK
Jul/21/2022 22:09:00 e-mail,debug send DATA
Jul/21/2022 22:09:00 e-mail,debug recv: 354 Start mail input; end with .
Jul/21/2022 22:09:00 e-mail,debug send .
Jul/21/2022 22:09:00 e-mail,debug recv: 250 Requested mail action okay, completed: id=1MjjCL-1nm8IB0jCF-00lG2o
Jul/21/2022 22:09:00 e-mail,info sent <> to: recipient@gmx.xxx
Jul/21/2022 22:09:00 e-mail,debug send QUIT
Jul/21/2022 22:09:01 e-mail,debug recv: 221 gmx.xxx Service closing transmission channel

So this looks ok,

I found this script and added it under the name checkip

:global ipadd;
:local thisip [/ip address get [find where interface=ether1] address];

:if ($ipadd != $thisip) do={
    /tool e-mail send to="recipient@gmx.xxx" subject="ip change" body="New ip $thisip";
    set ipadd $thisip;
}

when I do
/system script
run checkip

I see the script runs in the log but nothing happens.

Could anyone please give a pointer to why this script fails / does not run correctly?

My IP changes every few months. so I just want to get an email in the morning and when I see it I can take the needed actions.

greetings

Tsunamski

Most logical answer would be: because there is nothing to do !

Change (to test)
this: :if ($ipadd != $thisip)
to this: :if ($ipadd = $thisip)

So it will do something when the IP adress IS the same.
And then you should see the mail coming.

Don’t forget to set it back as it should be.

PS you may want to edit out the credentials in that log-part !

You can see my signature???

Do not post values that you do not know what are…

A�p�b�t�b�N�b�1������������=

j�n�i�s�o�e@gmx.net�!�3�q�4�7_J�n�i�s

� = censored…


CHANGE YOUR E-MAIL PASSWORD IMMEDIATELY!!!

I was hesitating to “report” but you beat me to it :laughing:

¯_(ツ)_/¯

I hope he read fastly, because bots are already indexed the page with the string on plaintext…

Also having several ip addresses on that interface makes the script fail…

@rextend, I think I dont understand some things here…
I changed my Password, fortunately this was a throwaway account just for sending purposes, I don’t use my main mail for test projects.
Simply forgot to change this, dont know why I added this line anyway.
Yes, I can read your signature, but I don’t get what I should take away from it.
Or am I just tired and not getting something?
Please explain.

@holvoetn
I made the change but no luck so far.
I am running 7.4rc2 if this makes a difference?

@eworm
no, just the public IP assigned to ether1 the private subnet is configerd on the bridge interface
it’s a very basic setup, thats why I dont understand the difficulty to get the address from ether1 and paste it to a mail

greetings Tsunamski

@Tsunamski, about the extra text in my signature, is a joke.

P.S.: Nice ‘Avacat’ :wink:

:global ipadd;

^ not needed

:local thisip [/ip address get [find where interface=ether1] address];

^ not needed

[find where interface=ether1] is one array with inside one ID for each IP

if only one ID is returned, because only one IP is present, “get” can get the IP correctly

get do not support multiple ID, if are present multimple IPs on that interface, you give error (invalid internal item number)

how solve

skip specific addresses like

[find where interface=ether1 and address!=“1.2.3.4/5” and address!=“6.7.8.9/10”]

or

always retrieve only first ID, but can be the wrong wanted IP…

:local thisip [/ip address get ([find where interface=ether1]->0) address]

also checking if is returned at least one ID, and exactly one, is better, because prevent errors if 0 IPs are assgned on that interface

:if ([:len [find where interface=ether1]] = 1) do={ } else={ }

:if ($ipadd != $thisip) do={
/tool e-mail send to=“recipient@gmx.xxx” subject=“ip change” body=“New ip $thisip”;

^ not needed

the e-mail send command suppose the correct configuration of the e-mail section

v Missing : before “set”

set ipadd $thisip;

^ not needed

}

Ok, I am reading this and get more and more question marks above my head.
Is there a documentation on scripting on Mikrotik? I think I need to get some explanations on these functions.
Because if I understood correctly the final code would look like this?

:local thisip [/ip address get ([find where interface=ether1]->0) address]
:if ([:len [find where interface=ether1]] = 1) do={ <ok> } else={ <something wrong> }
:if ($ipadd != $thisip) do={
    /tool e-mail send to="recipient@gmx.xxx" subject="ip change" body="New ip $thisip";
#                                                                                                           ^ not needed
# the e-mail send command suppose the correct configuration of the e-mail section

    v Missing : before "set"
    set ipadd $thisip;
#                    ^ not needed
}

And what I completely not understand is why the variable is not needed?
How else do I get the IP into the mail?
also I always get an auth failed while sending the mail via the send script and not with the winbox gui tool, I think something like TLS?yes is missing…

greetings Jenkins

ok…

i do not think you misunderstand everything…

:global ipadd;

^ not needed

= not needed what is pointed from the ^ symbol = “;” is not needed

:if ([:len [find where interface=ether1]] = 1) do={ } else={ }

this is an example, not a code…
and must be script codes, not literally “” and “”

v Missing : before “set”

set ipadd $thisip;

what mean??? is not a code, is a warning, miss “:” before “set”…

FWIW I tested the original code on my Hex (with the change of != to =) and it worked.

Hello all,

so I got it to work somehow in the end.
Thank you all for the help.
In the future I will go with more popular mail providers.

@rextended still trying to solve the signature, python only spits out garbage…

So do I mark this as solved or does it not need to be marked?

greetings

Tsunamski

Glad the issue is solved !

It’s not needed to mark as solved but it would help for others if you specify what exactly you had to change to make it work.
Surely sooner or later someone will run into the same problem, then it’s nice if the solution is also given.

So the solution in the end was:

rextendeds code:

:global ipadd;
#            ^ not needed

:local thisip [/ip address get [find where interface=ether1] address];
#                                                                    ^ not needed

# [find where interface=ether1] is one array with inside one ID for each IP
# if only one ID is returned, because only one IP is present, "get" can get the IP correctly
# get do not support multiple ID, if are present multimple IPs on that interface, you give error (invalid internal item number)

# how solve
# skip specific addresses like
# [find where interface=ether1 and address!="1.2.3.4/5" and address!="6.7.8.9/10"]
# or
# always retrieve only first ID, but can be the wrong wanted IP...
# :local thisip [/ip address get ([find where interface=ether1]->0) address]
#
# also checking if is returned at least one ID, and exactly one, is better, because prevent errors if 0 IPs are assgned on that interface
# :if ([:len [find where interface=ether1]] = 1) do={ <ok> } else={ <something wrong> }

:if ($ipadd != $thisip) do={
    /tool e-mail send to="recipient@gmx.xxx" subject="ip change" body="New ip $thisip";
#                                                                                     ^ not needed
# the e-mail send command suppose the correct configuration of the e-mail section

#   v Missing : before "set"
    set ipadd $thisip;
#                    ^ not needed
}

And the second problem were the correct settings for the mail Provider GMX:

address: IP Address here
port: 587
tls: starttls
vrf: main
from: youremail@gmx.xxx
user: youremail@gmx.xxx
password: yourpassword

I had troubles figuring out if the correct login was mail@provider.com@provider.com (some require this form of login) but in the end these settings work for GMX

greetings and many thanks

:unamused:
but… what… mah…