Editor's Note: Tip Top Tips is a semi-monthly column in The PCLinuxOS Magazine. Periodically, we will feature – and possibly even expand upon – one tip from the PCLinuxOS forum. The magazine will not accept independent tip submissions specifically intended for inclusion in the Tip Top Tips column. Rather, if you have a tip, share it in the PCLinuxOS forum's “Tips & Tricks” section. Occasionally, we may run a “tip” posted elsewhere in the PCLinuxOS forum. Either way, share your tip in the forum, and it just may be selected for publication in The PCLinuxOS Magazine.
This month's tip comes from SemperOSS, and was posted in the PCLinuxOS forum on June 30, 2024.
![The Web](images/page06/TTT01.webp)
Image by Mohamed Hassan from Pixabay
It seems that this subject has not been up since 2011, but I do not think it is less relevant now, so I would like to present a script that displays the external IP address (IPv4 only) using different methods and different external servers. I developed the method for a script that updates my DNS entry in my DNS provider's database. That script tries each server in order until it gets a reply, thus making it resilient to any individual site not working. The original script used lwp-request, but since this apparently had issues with missing dependencies on some machines, I have redone it using curl.
Most people will probably just use their browser for this, so I have added a list of websites I have used. Every website on the list works without needing JavaScript enabled, which I prefer for security reasons.
The script, as it is here, just shows the result from each source. If you find new sources, please send me a mail with the details and I will try to update the script at some point.
I call the script myip, and store it in ~/bin:
1. #!/bin/bash
2. PATH=/bin:/usr/bin
3. declare -A Sites
4. Sites=( \
5. [OpenDNS.com]='$( dig -4 +short myip.opendns.com @resolver1.opendns.com )' \
6. [WhoIsHostingThis.com]='$( curl -s https://www.whoishostingthis.com/tools/user-agent/ | awk '"'"'/flag.*[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*/ { sub(/.*flag[^0-9]*/, ""); sub(/<.*/, ""); print }'"'"' )' \
7. [ShowMyIP.com]='$( curl -s https://www.showmyip.com/ | awk '"'"'/[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*/ { sub(/[^0-9\.]*2>/, ""); sub(/<.*/, ""); print; exit }'"'"' )' \
8. [DynDNS.org]='$( curl -s checkip.dyndns.org | awk '"'"'{ sub(/.*:/, ""); sub(/<.*/, ""); print }'"'"' )' \
9. [CanHazIP.com]='$( curl -s http://canhazip.com )' \
10. [ShowIP.net]='$( curl -s http://showip.net/ )' \
11. [IPinfo.io]='$( curl -s https://ipinfo.io/ip )' \
12. [IPecho.net]='$( curl -s http://ipecho.net/plain)' \
13. [WhatIsMyIPaddress.com]='$( curl -s -A '"'"'Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0'"'"' https://whatismyipaddress.com/ 2>/dev/null | awk '"'"'/\/ip\// { sub(/.*none;.>/, ""); sub(/<.*/, ""); print; exit }'"'"' )' \
14. )
15.
16. MaxLength=0
17. for Site in "${!Sites[@]}"
18. do
19. if [ ${#Site} -gt $MaxLength ]; then
20. MaxLength=${#Site}
21. fi
22. done
23. MaxTabLength=$[ $MaxLength / 8 ]
24. for Site in "${!Sites[@]}"
25. do
26. Tabs=$[ $MaxTabLength - (${#Site} / 8) ]
27. echo -n $Site
28. while [ $Tabs -ge 0 ]
29. do
30. echo -n $'\t'
31. Tabs=$[ $Tabs - 1 ]
32. done
33. eval "IP=${Sites[$Site]}"
34. echo = $IP
35. done
Personally, I prefer the first method, where the dig command queries opendns.com directly. Thanks to opendns.com for making this hack available.
The other requests basically scrape websites for the information. If you only want one result, you can just remove the servers you do not want to use from the script.
List of websites:
• https://ipinfo.io/ip
• http://canhazip.com
• https://www.whoishostingthis.com/tools/user-agent/
• https://www.showmyip.com/
• http://checkip.dyndns.org/
• http://showip.net/
• https://whatismyipaddress.com/
You can download SemperOSS's script from the magazine website. As with all scripts hosted on the magazine website, save it to where you save all of your custom scripts, strip the “.txt” file extension, and mark the file as executable. Ideally, the location where you save your script should be in a directory that is in your PATH environment variables. That way, you can just call the script by its name, instead of having to type in the full path to the script (which is what you will have to do if you store it in a directory NOT in your PATH environment variables).
Editor's Note: If you use a VPN, you might also want to check to be sure you don't have a DNS leak. Visit https://www.dnsleaktest.com/ to check whether you have a DNS leak. A DNS leak can also reveal your external IP address, if you haven't taken efforts to also mask your DNS from the prying eyes of your ISP.
|