Getting your public IP address programmatically can be very useful. Especially if you need to know when your internet service provider (ISP) has changed your dynamic IP address. Here is how to get your public IP address using PowerShell: (Invoke-WebRequest myexternalip.com/raw).content. Read on for more background and details…
Other URLs exist which will provide you with your “raw” IP address without having to scrape through page tags. Just replace myexternalip.com/raw with any of the following:
(Invoke-WebRequest bot.whatismyipaddress.com).content (Invoke-WebRequest ident.me).content (Invoke-WebRequest api.ipify.org).content (Invoke-WebRequest ipconfig.me).content (Invoke-WebRequest ipecho.net/plain).content (Invoke-WebRequest ifconfig.me/ip).content
Some background info
In Windows you can run ipconfig and, in Linux, ifconfig, to get your IP address. If your computer is not sitting behind a router, ipconfig and ifconfig will return the public (internet) IP address provided by your Internet Service Provider (ISP). This IP address can also be referred to as the Wide Area Network (WAN) address. So far so good…
Most households and businesses, however, employ routers, which use Network Address Translation (NAT). NAT allows multiple devices to share a common “public”, or WAN, IP address. Unfortunately, when behind a router, the IP address you would get from ipconfig or ifconfig is the internal home or office network IP address of your computer, which is only visible to other internal home or office network devices.
When behind a router, in order to get the public IP address, you can log into your router and review the configuration. If this is not possible, you would need to get your public IP information from some other device that knows it. This device would essentially have to reside outside of your internal network, where your router’s external public network interface is visible.
There are many services that offer public IP discovery. Just Google “What Is My IP”. Google may even provide it directly in its search results. However, getting your public IP address programmatically may not be as easy. The actual IP address value is normally buried in a bunch of HTML code. This, then, requires scraping. Not fun.
Well, here’s an easy way to get your public IP address using PowerShell…
How do I get my public IP address Using PowerShell?
Note: Please disregard the .555. It’s for demonstration purposes only…As you can see, it’s very simple. We use Invoke-WebRequest against myexternalip.com/raw. Again, we can also run Invoke-WebRequest against other “What’s My IP” type URLs, but those usually have to be scraped for the IP address value. Please note Invoke-WebRequest requires PowerShell version 3.0 or later.
You can accomplish this using any language that can perform a web request. It doesn’t have to be PowerShell!
Another easy way to retrieve your public IP address using PowerShell is by running the Invoke-RestMethod command against http://ipinfo.io/json. That is,
(Invoke-RestMethod http://ipinfo.io/json).ip. You can also get other IP information such as hostname, city, country, and geolocation (latitude and longitude). See https://ipinfo.io for more info.
$ip = Invoke-RestMethod http://ipinfo.io/json $ip.ip $ip.hostname $ip.city $ip.region $ip.country $ip.loc $ip.org $ip.postal $ip.readme
Happy scrapeless scripting…