Page 1 of 1

Howto get the PTR record for a single IP?

Posted: Thu Nov 25, 2021 12:22 pm
by danergo
Folks,

I'm looking for pretty obvious need: getting the PTR (string) for an IP address in my script.

For example: having "18.196.34.14" in a variable, and I want to get it's PTR (name): "ec2-18-196-34-14.eu-central-1.compute.amazonaws.com."
(even if it's a non-authoritative answer)

How can I do it with scripts? Or I shall use an external online service for this?

Re: Howto get the PTR record for a single IP?

Posted: Thu Nov 25, 2021 12:38 pm
by ConnyMercier
Try the :resolve command

Exemple :

[admin@MikroTik] > :put [:resolve 18.196.34.14]
ec2-18-196-34-14.eu-central-1.compute.amazonaws.com

Re: Howto get the PTR record for a single IP?

Posted: Thu Nov 25, 2021 1:30 pm
by Jotne
To use it in the script
:local ptr  [:resolve 18.196.34.14]
This will resolve the IP and save it to the variable ptr

Re: Howto get the PTR record for a single IP?

Posted: Thu Nov 25, 2021 1:31 pm
by danergo
OMG, thank you.
(I read a lot of old post and by that time this was unsupported)

Re: Howto get the PTR record for a single IP?

Posted: Thu Nov 25, 2021 1:37 pm
by danergo
To use it in the script
:local ptr  [:resolve 18.196.34.14]
This will resolve the IP and save it to the variable ptr

Thank you! How can I make sure to not crash my script if I give it some invalid address? Like:
:local ptr  [:resolve 192.168.88.100]
?

Re: Howto get the PTR record for a single IP?

Posted: Thu Nov 25, 2021 1:44 pm
by danergo
I just have to put it into ":do" ?

Re: Howto get the PTR record for a single IP?

Posted: Thu Nov 25, 2021 7:52 pm
by Jotne
You can use
on-error
:do {
	:local ptr  [:resolve 192.168.88.100]
	:put $ptr
} on-error={
	:put "Could not resolve"
}