Skip to content

Instantly share code, notes, and snippets.

@bogdangrigg
Last active March 20, 2020 09:19
Show Gist options
  • Save bogdangrigg/d64d45280b5adbf12e1feb2d476ecc0d to your computer and use it in GitHub Desktop.
Save bogdangrigg/d64d45280b5adbf12e1feb2d476ecc0d to your computer and use it in GitHub Desktop.
Returns the public / Internet-facing IP used by a host. Be careful with multiple outgoing IPs.
### WINDOWS ###
# option #1: generic version, from here: https://gallery.technet.microsoft.com/scriptcenter/Get-ExternalPublic-IP-c1b601bb
Invoke-RestMethod http://ipinfo.io/json | Select -exp ip
# option #2: Azure metadata service
(Invoke-RestMethod -H @{'Metadata'='true'} "http://169.254.169.254/metadata/instance?api-version=2017-08-01").network.interface.ipv4.ipAddress.publicIpAddress
### LINUX ###
dig +short myip.opendns.com @resolver1.opendns.com
host myip.opendns.com resolver1.opendns.com | grep "myip.opendns.com has" | awk '{print $4}'
wget -qO- http://ipecho.net/plain | xargs echo
wget -qO - icanhazip.com
curl ifconfig.co
curl ifconfig.me
curl icanhazip.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment