I regularily have the need to fetch the public IPv4 of a server that is natted.
The best service to use for my needs is https://ipinfo.io/.
You can even use it with curl on a server and receive the data in json:
curl -v -4 ipinfo.io | jq .
{
"ip": "193.170.1.2",
"city": "Vienna",
"region": "Vienna",
"country": "AT",
"loc": "48.2085,16.3721",
"org": "AS1853 ACONET",
"postal": "1010",
"timezone": "Europe/Vienna",
"readme": "https://ipinfo.io/missingauth"
}
You can even add a IP to the query to fetch the info for this IP:
curl -v -4 ipinfo.io/193.170.1.2 | jq .