Obtain global IP in bash

In current Internet environment, every PC is assigned a private IP. If global IP is necessary, the router the PC is connecting has the IP. But accessing the router typically needs the password to access that should not appear on a bash script.

If you access http://checkip.dydns.com/, it returns the global IP, then the bash script to obtain global IP looks like the following.

ip=$(w3m -dump http://checkip.dyndns.com/ | grep -oP '\d+\.\d+\.\d+\.\d+')
  • w3m: text browser
  • -dump: shows the response and quit
  • -o: returns the matched text
  • -P: uses Perl regex
  • \d: represents a number
  • +: continuous number
  • \.: . itself

Leave a comment

Your email address will not be published. Required fields are marked *