Yep, yours is way better than relying on a third party service.
I use a similar script, but for the IP address on an eth port so I end up with an internal address in my domain. It uses an ifconfig command, I believe.
In your script, I tried dig +short myip.opendns.com @resolver1.opendns.com
, but the result was blank. When I use the curl command, it does return my IP address like so:
123.123.123.123dnslookup: 0.061256 | connect: 0.153074 | appconnect: 0.387071 | pretransfer: 0.387107 | starttransfer: 0.484302 | total: 0.484332 | size: 13
Is this the behavior you were expecting?