Home > Lync Server/Skype for Business Server > One Liner: Getting Your Public IP Address With PowerShell

One Liner: Getting Your Public IP Address With PowerShell

My friend Matt (@MatthewDippel) was espousing the clean results that https://wtfismyip.com provides over more popular sites like http://www.whatismyip.com. Matt noted that other than the explicit language on wtfismyip.com, the results were cleaners and easier to use that others. Matt’s a developer, so I had to give him some grief that he used a web page instead of writing some code. I figured I should share that code here, because in ten minutes, I’ll forget what it was, and need to look it up some time in the future.  🙂

The information is fairly straight forward to retrieve, assuming you can find a source that provides it. dnsomatic.com is one such site. If you see the documentation for developers at http://www.dnsomatic.com/wiki/api, you can see that they provide an example of browsing to http://myip.dnsomatic.com returns the public IP of the client requesting the page. That being the case, we can use something simple like invoking a web request in PowerShell to get that data. Easy peasy. We can just look at the content value of the returned results:

(Invoke-WebRequest 'http://myip.dnsomatic.com').Content

And we see that it returns just a simple text value of our public IP address:

VERBOSE: GET http://myip.dnsomatic.com/ with 0-byte payload
VERBOSE: received 13-byte response of content type text/html
173.10.47.133

Now, we can expand on that a bit, albeit getting away from the one-liner concept. We can take the results of the above query and send them to the GEO IP service at webservicex.net and get some geo info:

$ip = (Invoke-WebRequest "http://myip.dnsomatic.com").Content
$geoip = New-WebServiceProxy "http://www.webservicex.net/geoipservice.asmx?WSDL"
$geoip.GetGeoIP($ip)

For more info on WebserviceX’s geo lookup, check out http://www.webservicex.net/New/Home/ServiceDetail/64.They’ve got some other cool solutions, including retrieving weather data, at http://www.webservicex.net/new/Home/Index.

And what we see here is

VERBOSE: GET http://myip.dnsomatic.com/ with 0-byte payload
VERBOSE: received 13-byte response of content type text/html
ReturnCode        : 1
IP                : 173.10.47.133
ReturnCodeDetails : Success
CountryName       : United States
CountryCode       : USA

We see where we get the country name and code as well.

BTW – I found another source of IP info that also doesn’t require cleanup. And that is Matt’s new favorite, wtfismyip.com.

(Invoke-WebRequest 'https://wtfismyip.com/text').Content

I’m happy to add others to this list if you send me the info.

You’ll notice the verbose output in the above examples. If you want to suppress the verbose output, we can do that with the -Verbose parameter, setting it to $false:

(Invoke-WebRequest 'https://wtfismyip.com/text' -Verbose:$false).Content

As you can see, getting your public IP address is fairly straightforward.

  1. Arturas
    January 15th, 2018 at 03:27 | #1

    You can also use json.

    $PubIPData = (Invoke-WebRequest ‘https://wtfismyip.com/json’).Content|ConvertFrom-Json

  1. No trackbacks yet.