Rob van der Woude's Scripting Pages

VBScript Scripting Techniques > Internet > WAN IP address
VBScript Scripting Techniques > Network > WAN IP address

Retrieve your WAN IP address

 

  1. WinHTTP
  2. XMLHTTP
  3. Internet Explorer
  4. Sample usage

 

WinHttp.WinHttpRequest.5.1
VBScript Code:
Function MyIP_WinHTTP( )
' Name:       MyIP_WinHTTP
' Function:   Display your WAN IP address using WinHTTP
' Usage:      ret = MyIP_WinHTTP( )
' Returns:    WAN (or global) IP address
'
' This script uses WhatIsMyIP.com's automation page
' http://automation.whatismyip.com/n09230945.asp
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com
    Dim lngStatus, objHTTP, objMatch, objRE, strText, strURL
    ' Return value in case the IP address could not be retrieved
    MyIP_WinHTTP = "0.0.0.0"
    ' Retrieve the URL's text
    strURL = "http://automation.whatismyip.com/n09230945.asp"
    Set objHTTP = CreateObject( "WinHttp.WinHttpRequest.5.1" )
    objHTTP.Open "GET", strURL
    objHTTP.Send
    ' Check if the result was valid, and if so return the result
    If objHTTP.Status = 200 Then MyIP_WinHTTP = objHTTP.ResponseText
    Set objHTTP = Nothing
End Function
Requirements:
Windows version: Windows 2000 SP3 or later
Network: any
Client software: Internet Explorer 5.01
Script Engine: any
Summarized: Works in Windows 2000 SP3 or later.
Should work in Windows 95, 98, ME, or NT 4 with Internet Explorer 5.01 or later.
 
[Back to the top of this page]
 
Microsoft.XMLHTTP
VBScript Code:
Function MyIP_XMLHTTP( )
' Name:       MyIP_XMLHTTP
' Function:   Display your WAN IP address using XMLHTTP
' Usage:      ret = MyIP_XMLHTTP( )
' Returns:    WAN (or global) IP address
'
' This script uses WhatIsMyIP.com's automation page
' http://automation.whatismyip.com/n09230945.asp
'
' Original script written in JScript by Isaac Zelf
' "Translated" to VBScript by Rob van der Woude
' http://www.robvanderwoude.com
    Dim objRequest, strURL
    ' Return value in case the IP address could not be retrieved
    MyIP_XMLHTTP = "0.0.0.0"
    ' Retrieve the URL's text
    strURL = "http://automation.whatismyip.com/n09230945.asp"
    Set objRequest = CreateObject( "Microsoft.XMLHTTP" )
    objRequest.open "GET", strURL, False
    objRequest.send vbNull
    If objRequest.status = 200 Then MyIP_XMLHTTP = objRequest.responseText
    Set objRequest = Nothing
End Function
Requirements:
Windows version: any
Network: any
Client software: Internet Explorer 5 or later
Script Engine: any
Summarized: Works in any Windows version with Internet Explorer 5 or later.
 
[Back to the top of this page]
 
InternetExplorer.Application
VBScript Code:
Function MyIP_IE( )
' Name:       MyIP_IE
' Function:   Display your WAN IP address using Internet Explorer
' Usage:      ret = MyIP_IE( )
' Returns:    WAN (or global) IP address
'
' This script uses WhatIsMyIP.com's automation page
' http://automation.whatismyip.com/n09230945.asp
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com
    Dim blnTimedOut, i, objIE, objMatch, objRE, strText, strURL
    ' Return value if IP address couldn't be retrieved
    MyIP_IE = "0.0.0.0"
    ' Open the appropriate URL in Internet Explorer
    strURL = "http://automation.whatismyip.com/n09230945.asp"
    Set objIE = CreateObject( "InternetExplorer.Application" )
    objIE.Visible = False
    objIE.Navigate2 strURL
    ' Wait till IE is ready
    i = 0
    blnTimedOut = False
    Do While objIE.Busy
        WScript.Sleep 100
        i = i + 1
        ' Time out after 10 seconds
        If i > 100 Then
            blnTimedOut = True
            Exit Do
        End If
    Loop
    ' Retrieve the URL's text
    If Not blnTimedOut Then MyIP_IE = objIE.Document.Body.InnerText
    ' Close the Internet Explorer session
    objIE.Quit
    Set objIE = Nothing
End Function
Requirements:
Windows version: Windows 98 or later
Network: any
Client software: Internet Explorer
Script Engine: any
Summarized: Works in Windows 98 and later with Internet Explorer.
 
[Back to the top of this page]
 
Sample usage
VBScript Code:
Option Explicit

Dim dtmStart, lngSeconds

WScript.Echo "Comparing 3 ways to retrieve your WAN IP address:" & vbCrLf

dtmStart = Now
WScript.Echo "InternetExplorer.Application    " _
           & MyIP_IE( )      & "    (" _
           & DateDiff( "s", dtmStart, Now ) & " seconds)"

dtmStart = Now
WScript.Echo "Microsoft.XMLHTTP               " _
           & MyIP_XMLHTTP( ) & "    (" _
           & DateDiff( "s", dtmStart, Now ) & " seconds)"

dtmStart = Now
WScript.Echo "WinHttp.WinHttpRequest.5.1      " _
           & MyIP_WinHTTP( ) & "    (" _
           & DateDiff( "s", dtmStart, Now ) & " seconds)"
 
Sample output:
Comparing 3 ways to retrieve your WAN IP address:

InternetExplorer.Application    18.117.142.128    (2 seconds)
Microsoft.XMLHTTP               18.117.142.128    (1 seconds)
WinHttp.WinHttpRequest.5.1      18.117.142.128    (0 seconds)

 


page last modified: 2016-09-19; loaded in 0.0032 seconds