Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get WAN IP from router 3

Status
Not open for further replies.

waytech2003

Programmer
Jul 14, 2003
316
0
0
US
I want to get the WAN IP address from my DSL router. I figured I could just take it from the DSL box as a html doc. I find the information at the webpage below.


I need to get that page, using VB6, then I would find what I need in the html text. Problem is I have no idea how to go about this. Should I use INet, webBrowser, or some other control. A sample to get me stared would be appreciated. Once I have the document, I know how to get the info I need from it.

Also, if there is a better way to find the WAN IP, in code, I would like to know that too.

Wayne
 
Using the webbrowser control; add the "Microsoft Internet Controls" component, and the following code should get you much of the way there:
Code:
Private Sub Form_Load()
   WebBrowser1.Navigate ("[URL unfurl="true"]http://192.168.254.254/summary.htm")[/URL]
End Sub

Private Sub WebBrowser1_NavigateComplete2(ByVal pDisp As Object, URL As Variant)
   sHTML = WebBrowser1.Document.documentElement.InnerHTML
   [COLOR=green]'parse sHTML, finding the part that contains your public IP address[/color]
End Sub

If you want a solution that works on networks other than your own, navigate to " instead (or any other various sites that tell you the public ip of the computer you are on).

I think you'll find this method is the best (easiest to implement, works on the most networks, regardless of router used, etc)
 
Thank you, that works very well. I think I will use the "WhatsMyIP" site to get my Public IP. As you said, that way I do not have to worry about different hardware setups.

Wayne
 
To help anyone who wants to find their public WAN IP, I am posting the code I am using. Your IP number will be in the Form's Caption. Thanks to guitarzan for getting me started in the right direction.

Private Sub Form_Load()
WebBrowser1.Navigate ("End Sub

Private Sub WebBrowser1_TitleChange(ByVal Text As String)
Me.Caption = Right(Text, InStr(Text, " ") - 2)
End Sub
 
And, of course, there's very little we haven't covered in this forum over the years. A quick search would have turned up thread222-694235 which gives my whatsmyip solution (using the more lightweight Inet control) ...
 
strongm

Thanks for pointing to that older thread. Before I started a new question, I did a search for "WAN IP" and "Public IP" and did not find anything that I could use.

Guess this one will get found if anyone looks in the future. [2thumbsup]

 
Thanks strongm, I didn't know that about the Inet control.

Have a star for that pointer :)
 
I have found this post really helpful in a little project i was writting. Only problem is i cant seem to get webbrowser control events to fire?

I am using VB6 and have the reference to microfot internet controls added. I am also running ie1.visible = true in case that may have been effect the events.

HAve tried both titlechange and documentcomplete. I'm a noob so most likely i'm doing/forgetting something obvious.

Any help appreciated
 
You could try using an API call to get the information. It doesn't require an external dependency to run. The code I am showing does use an external component (the file system object) to open the downloaded file. Since I'm using late binding, you don't even need to add a reference to it. Here's the code.

First, you will need to declare the API function. So... put this code at the very top of the code module that you are working with.

Code:
Option Explicit

Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

Then, to get the IP address, use this code.

Code:
Dim cTemp As String
Dim arTemp() As String

Call URLDownloadToFile(0, "[URL unfurl="true"]http://www.whatismyip.com",[/URL] App.Path & "\ipaddress.htm", 0, 0)
If Dir(App.Path & "\ipaddress.htm") <> "" Then
    cTemp = CreateObject("Scripting.FileSystemObject").OpenTextFile(App.Path & "\ipaddress.htm").ReadAll
    If InStr(cTemp, "<h1>") > 0 Then
        arTemp = Split(Replace(cTemp, "</h1>", "<h1>"), "<h1>")
        MsgBox arTemp(1)
    Else
        MsgBox "Unknown IP Address"
    End If
    Kill App.Path & "\ipaddress.htm"
Else
    MsgBox "Unknown IP Address"
End If

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Try getting results in a Message box as below, add see if that works. I assume that your Webbrowser Control is being called "ie1"

Private Sub ie1_TitleChange(ByVal Text As String)
MsgBox Right(Text, InStr(Text, " ") - 2)
, vbOKOnly

End Sub
 
Thanks for the replies gmmastros and waytech2003.

waytech2003,

I had already tried solutions along these lines but the problem is the event isn't firing. I know this because even if i use the following:

msgbox ("hello")

In the Sub, i dont get the message "hello"!


gmmastros,

Your solution works a treat, most grateful.
Not wishing to seem ungrateful, but i would still like to understand why the events don't appear to firing? As far as my understanding goes both the events i listed should work with VB6 providing the relevant reference is added. So if anyone has any thoughts on this aspect, please post.

again many thanks to those who replied
 
I know you have references set. I just wanted to make sure you added a WebBrowser control on your form. By default it would be called WebBrowser1, and that is where the Website would load. Do you see the website in that browser?
 
And the penny drops!! thanks waytech2003 that little seed was what i needed. I hadn't added the MS internet control component. Unforunately for a beginner, searching the net for answers is full of pitfalls. A lot of explinations assume everyone is not a beginner therefore forget to explain that their example is for dot net 2 or you need to add internet control component, for example ;)

But my question is answered with help from you and gmmastros, plus i got another way to find the wan ip as well.

Case closed, happy customer, Many thanks
 
Or one might use AsyncRead with a UserControl to avoid dependencies too.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top