Community discussions

MikroTik App
 
jecoronas
just joined
Topic Author
Posts: 5
Joined: Sun Mar 03, 2019 5:39 pm

Send Public IP to telegram

Thu Mar 31, 2022 7:41 am

Hi everyone,
How can i get the public IP and send it to telegram?. I need to know when that IP changes and then put it on the destination address on NAT firewall rule in order to make a port redirection for a server at my home. Thanks...
 
AidanAus
Member Candidate
Member Candidate
Posts: 177
Joined: Wed May 08, 2019 7:35 am
Location: Australia
Contact:

Re: Send Public IP to telegram

Thu Mar 31, 2022 8:21 am

I think a possible solution would be put DDNS on the router you want to get the IP address of, after that on the router on the other side make an address list and put in that DDNS name under the address field then put in a name for that address list.
From there you can change your dst-nat address from using the single IP address and use the new IP address list instead :)

The only thing I am not sure about is how long the entry's stay, I would presume that they get updated with the TTL timeout in the DNS cache?
 
jecoronas
just joined
Topic Author
Posts: 5
Joined: Sun Mar 03, 2019 5:39 pm

Re: Send Public IP to telegram

Thu Mar 31, 2022 8:29 am

Thanks, I´m using the mikrotik cloud DDNS, but with an especific port for some extra security. I want to get with a script is get the public IP and send it to Telegram
 
AidanAus
Member Candidate
Member Candidate
Posts: 177
Joined: Wed May 08, 2019 7:35 am
Location: Australia
Contact:

Re: Send Public IP to telegram

Thu Mar 31, 2022 8:36 am

I dont think you need to if both sides are Mikrotik devices since you can make use of the address list function stated in the last message, but everyone has their own ways :)
You can find the solution to your method in this forum post:
viewtopic.php?t=173773
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Send Public IP to telegram

Thu Mar 31, 2022 9:07 am

Turn in IP Cloud on your router and get IP using this command.
:put [/ip/cloud/get public-address]
 
AidanAus
Member Candidate
Member Candidate
Posts: 177
Joined: Wed May 08, 2019 7:35 am
Location: Australia
Contact:

Re: Send Public IP to telegram

Thu Mar 31, 2022 9:13 am

Ok so I would put that script in the put here secton of this one, havent testing it but at least this can start getting you to where you need to go, you need to keep the ; as that tells the router its the end of that command.
Since the Ip address is stored in variables the global variable will be wiped on reboot so that is why we make it each time, trying to make it again once its alreay stated doesnt look to make it loose its value.
the current address doesnt need to be stored in a global either as we are checking it each time we run this, local variables will be removed once that run environment is gone, that means if you want to run this manually through the terminal you will need to put it all in {}

the other thing to get this to run is I would suggest putting this into system scripts once you have added the telegram stuff, then adding a scheduler to run that script, simple as run (sciptname), this just gives you some options to troubleshoot the script by running it manually rather than waiting for however long you set the script interval to be.

I think anav's single line of code should be ok to send through just in the message put the variable name $cAdd and it should hopefully just message you the ipadress but IDK as i dont use this platform so I cant test it :)

global pAdd
local cAdd
set cAdd [ip address find interface=!!!WAN INTERFACE!!!]
set cAdd [ip address get number=$cAdd value-name=address ]
if ($cAdd!=$pAdd) do={ !!!!PUT HERE!!!!;set $pAdd $cAdd
}
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Send Public IP to telegram

Thu Mar 31, 2022 1:27 pm

local cAdd
set cAdd [/ip/cloud/get public-address]
 
K0NCTANT1N
newbie
Posts: 48
Joined: Thu Jun 08, 2023 9:35 pm

Re: Send Public IP to telegram

Mon Aug 07, 2023 9:51 pm

For MT, I'm writing one of the first codes today.

Today in this script I will send ONLY information for this moment.
:local token "--49466755:AAFG1PPSJYfug2mhMT_ArgF1ZKHg1hbKhE--"
:local chatID "--54021--"
:local pAdd [/ip/cloud get public-address]
:local text "\E2\84\B9 $[/system identity get name] $[/system resource get board-name]%0A%0APublic Address: $[/ip/cloud get public-address]%0AName: $[/ip/cloud get dns-name]%0AStatus: $[/ip/cloud get status]"

/tool fetch url="https://api.telegram.org/bot$token/sendMessage?chat_id=$chatID&text=$text" output=user
But in the future, I want to save the address this moment in memory, and after 30 minutes compare with the current one.

Thanks.
 
K0NCTANT1N
newbie
Posts: 48
Joined: Thu Jun 08, 2023 9:35 pm

Re: Send Public IP to telegram

Tue Aug 08, 2023 5:53 pm

After reading it seems like there's no easy way outside of going for scripting: https://wiki.mikrotik.com/wiki/Manual:S ... ve_changed
:global currentIP;

:local newIP [/ip address get [find interface="ether1"] address];

:if ($newIP != $currentIP) do={
    :put "ip address $currentIP changed to $newIP";
    :set currentIP $newIP;
}
But $currentIP is always empty
:global currentIP;

:local newIP [/ip/cloud get public-address];

:local token "--49466755:AAFG1PPSJYfug2mhMT_ArgF1ZKHg1hbKh--";
:local chatID "--54021--";
:local text "\E2\84\B9 $[/system identity get name] $[/system resource get board-name]%0A%0APublic Address: $[/ip/cloud get public-address]%0AName: $[/ip/cloud get dns-name]%0AStatus: $[/ip/cloud get status]";

:if ($newIP != $currentIP) do={
    /tool fetch url="https://api.telegram.org/bot$token/sendMessage?chat_id=$chatID&text=$text" output=user;
    :set currentIP $newIP;
}
Please share if there are better ways to achieve this - seems like
Last edited by K0NCTANT1N on Wed Aug 09, 2023 4:15 pm, edited 1 time in total.
 
K0NCTANT1N
newbie
Posts: 48
Joined: Thu Jun 08, 2023 9:35 pm

Re: Send Public IP to telegram

Wed Aug 09, 2023 11:04 am

Does anyone have any ideas?
Last edited by K0NCTANT1N on Wed Aug 09, 2023 2:12 pm, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send Public IP to telegram

Wed Aug 09, 2023 12:55 pm

Yes, mine is that you like to self-quote unnecessarily and, to me, this mess disgusts me and doesn't make me want to help.
 
K0NCTANT1N
newbie
Posts: 48
Joined: Thu Jun 08, 2023 9:35 pm

Re: Send Public IP to telegram

Wed Aug 09, 2023 1:06 pm

Unspoken orders?

Maybe I did wrong a little, but that's not the ultimate crime.

Thank you. I'm sorry
 
K0NCTANT1N
newbie
Posts: 48
Joined: Thu Jun 08, 2023 9:35 pm

Re: Send Public IP to telegram

Fri Aug 18, 2023 5:52 pm

After specifying "Policy": "read, write, policy, test", the variable is stored in "Environment" and everything works correctly.

Thanks to all.

Who is online

Users browsing this forum: No registered users and 13 guests