Getting Local & Public IP Address

Started by KeithW, April 12, 2018, 06:47:57 PM

Previous topic - Next topic

KeithW

I thought I had found set of posts about this and some examples, etc but I have just spent the last 30 minutes searching and cannot seem to turn it up.   Has anyone developed a chunk of code that gets the local & public IP address for a system?  I have a system that is on a NAT LAN and due to reboots and other changing things I need to easily get the associated IPs.  Hostname & MAC would be a plus but not required.

Thanx,
Keith

td

How do I count the ways?   There is a whole section in the Tech Database entitled "Get IP Address" that has a half dozen or so methods and that doesn't even cover all of the possibilities.  Admittedly, the examples are a bit dated but are still viable.  The link to the IP Grabber extender is outdated but you can find the extender on our Download page.   The extender can also be used to find the hostname and MAC address as well.  There should also be some WMI examples in the Tech Database that can be used to obtain the same information.   

One of the most common ways to get the external IP address is to place an HTTP request to one of the public sites that are set up for that exact purpose.   

Search on "IP Address" and some other terms in the Tech Database and pick your preferred solutions.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

snowsnowsnow

The take-away is this:

1) There are, as you say, a million and one ways to get the local IP address.

2) There is exactly one way of getting the external IP address that doesn't involve the cooperation of some external site.  That way is to query the router and make it tell you what it's external IP address is.

3) If you can assume the cooperation of an external site, there are, again, a million ways to do it.

td

I suppose you could argue that under certain network configurations you could simply perform a DNS lookup to get the external IP address even when the computer of interest is receiving its IP address from a DHCP server.   Mostly it would require a static IP and a public domain name or using some kind of dynamic DNS server.  Whether or not the DNS lookup constitutes using an external site would depend on whether or not you do your own DNS hosting.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

KeithW

Quote from: snowsnowsnow on April 13, 2018, 08:29:47 AM
The take-away is this:

1) There are, as you say, a million and one ways to get the local IP address.

2) There is exactly one way of getting the external IP address that doesn't involve the cooperation of some external site.  That way is to query the router and make it tell you what it's external IP address is.

3) If you can assume the cooperation of an external site, there are, again, a million ways to do it.


OK, I found some scripts and modified them to get my local LAN Addr, currently I have a static IP addr and I want to be able to retrieve
the Public IP which may or may not be static in the future... how would I query the router for that info?  I am aware of sites such as
whatsmyip.com & org... but they bury in the return page or use additional references to the info making it a nightmare to extract that
data from the webpage....  is there a way to get Public IP and possibly DNS info... the script I am working will also be run on a couple
of Dynamic IP'd machines, so just because I know my info, it does not help if I cannot retrieve the info for the machine running the script.

Keith

ChuckC

Once you have the public IP address, a simple reverse DNS lookup will return whatever authoritative information has been registered with a DNS server that hosts the zone for the IP subnet in question.

Regarding the issue of the public IP address being embedded in a web page that you have to parse, that's one of the burdens you take on when trying to "screen scrape", or, in this case, "web scrape" something human-readable for "structured" data.  Fortunately, many times, the information that you want may also be available from the same web site but via a web API call, such as REST, SOAP or XML RPC.  Take a look at the following URL for more information about the subject:

https://www.whatismyip.com/api/

Please note that some service providers may charge for the use of the programming API since it is likely to put a much higher load [via rapidly repeated usage] on their servers as compared to casual web page requests for the human-readable form of the information.  With What's My IP, the web page to register for an API key doesn't seem to indicate any pricing, so their registration process may simply be a way of responding to abusive use of the API by banning or black listing your name & email address from future registration attempts if your current API key gets deactivated for abuse.


KeithW


td

Quote
OK, I found some scripts and modified them to get my local LAN Addr, currently I have a static IP addr and I want to be able to retrieve
the Public IP which may or may not be static in the future... how would I query the router for that info?  I am aware of sites such as
whatsmyip.com & org... but they bury in the return page or use additional references to the info making it a nightmare to extract that
data from the webpage....  is there a way to get Public IP and possibly DNS info... the script I am working will also be run on a couple
of Dynamic IP'd machines, so just because I know my info, it does not help if I cannot retrieve the info for the machine running the script.

Not all sites "bury in the return page or use additional references".  Several sites provide the external IP address as the only content returned from an HTTP "Get" verb request.   A total of 6 lines at the most.  One such site is myexternalip.com.
Code (winbatch) Select
ObjHttp = ObjectCreate("WinHttp.WinHttpRequest.5.1")

TimeoutVal = 59000
ObjHttp.SetTimeouts(TimeoutVal, TimeoutVal, TimeoutVal, TimeoutVal)   
ObjHttp.Open("GET", "http://www.myexternalip.com/raw")
ObjHttp.Send()

strIP = ObjHttp.ResponseText


With regard to DNS it all depends on what you mean by "Dynamic IP'ed" machines.  If are referring to machines obtaining their IP address via DHCP then it depends on whether or not the network edge router has a fixed external IP address and that IP address is associated with a domain.  If those two conditions are true, you will likely be able to perform a simple DNS query on the domain to get the IP address.  Obviously, this type of solution is limited to a unique set of circumstances. 

DNS queries - both forward and reverse lookup - can easily be performed using either the IP Grabber, WinSock, or WinInet Extender.

[edit]: Of course, using a VPN would blow the fixed IP address approach out of the water and you would be back to an external host approach.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

KeithW

How Cool !!

This does exactly what I was looking for.  I had never played with the CreateObject command, I can see uses for this in my future.
How do you go about determining if a site returns the IP info in the way this referenced site does?  IS there a specific set of terms
one would have to query for find sites like this.  Reason I ask is if one would go away, how would you find another?

Thanx for the lesson !
Keith

td

There are a lot of sites providing your external IP address.  To find them you use search terms like "get external IP address" in your search engine of choice.  You can probably come up a with a better set of terms to narrow your search.  Generally, you have to go to a found site to determine whether or not it provides a clean IP address response.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade