Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to read external WAN IP?

353 views
Skip to first unread message

Dave "Crash" Dummy

unread,
Mar 6, 2013, 10:47:43 AM3/6/13
to
I have a NAT router, and when I use ipconfig.exe, all I can see is the
local LAN IP (192.18.1.xx). I can see the external WAN address in the
router firmware (Tomato) GUI, but I don't know how to access that from
script. I would like to read my external IP from a script. Any suggestions?

Thanks!

P.S. I am currently using a utility on a remote server to read the
address. I want to keep everything "in house."
--
Crash

"In politics, stupidity is not a handicap."
~ Napoleon Bonaparte ~

X22degg5

unread,
Mar 8, 2013, 9:20:43 PM3/8/13
to

"Dave "Crash" Dummy" <inv...@invalid.invalid> wrote in message
news:kh7ocb$ofq$1...@dont-email.me...
>I have a NAT router, and when I use ipconfig.exe, all I can see is the
> local LAN IP (192.18.1.xx). I can see the external WAN address in the
> router firmware (Tomato) GUI, but I don't know how to access that from
> script. I would like to read my external IP from a script. Any
> suggestions?
>
> Thanks!
>

I don't have your modem, but on my modem, the IP can be drawn from a web
page.
So, taking a look at my web page, I see something like:
<td class="indent">IP Address</td>
<td>255.255.25.2</td>

So, I could use the Internet Explorer object to pull in the web page, but I
rather use the lighter weight object Microsoft.XMLHTTP.

So, to pull it from my modem, my slap it together code is:
-----
Set objHttp = CreateObject("Microsoft.XMLHTTP")
objHttp.Open "GET", "http://192.168.1.254/xslt?PAGE=C_1_0", False
objHttp.Send
ModemText = CStr(objHttp.ResponseText)
Modemtext2 = Replace(ModemText, vbcrlf, "")
position = InStr(ModemText2, "IP Address")
If position <> 0 Then
IpADDr = split(mid(Modemtext2, position +38, 15), "<")
wscript.echo IPAddr(0)
end if
----

Notes: The actual IP address is different. Since IP addresses can vary in
length, I needed to deal with the tailing "<" in the web page.
I don't know what things look like with your modem, but Microsoft.XMLHTTP
would be on my short list. My modem only has HTTP access.
OS is XP3


Dave "Crash" Dummy

unread,
Mar 9, 2013, 7:42:33 AM3/9/13
to
Thank you. My WebStar modem is accessible at 192.168.100.1,
but it does not display the IP, just the MAC address.

Dave "Crash" Dummy

unread,
Mar 9, 2013, 9:17:25 AM3/9/13
to
This is by no means a one size fits all solution. I slapped together
some code
to read the GUI interface for my Tomato router firmware:

function WAN()
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "http://192.168.1.1/"
Do Until IE.readyState = 4 : WScript.Sleep 10 : Loop
txt=IE.document.getElementsByTagName("div")(6).innerText
WAN=split(txt,"IP Address")(1)
WAN=split(WAN,vbCRLF)(0)
end function

--
Crash

"The future ain't what it used to be."
~ Yogi Berra ~

Mayayana

unread,
Mar 9, 2013, 9:19:15 AM3/9/13
to
I'm not sure if this is what you want, but just in case....

If the router is 192.168.1.1, and your local IP is 192.168.1.50,
and the router external (as seen by websites), is 100.80.60.40,
the following will return 192.168.1.50. (I don't know the proper
terminology to distinguish the addresses.)

Dim WMI, Col, Ob, S2

Set WMI = GetObject("WinMgmts:")

Set Col = WMI.ExecQuery("Select * from Win32_NetworkAdapterConfiguration
WHERE MACAddress > '' AND IPEnabled = True")
For Each Ob in Col
If IsArray( Ob.IPAddress) Then
If UBound( Ob.IPAddress) = 0 Then
S2 = Ob.IPAddress(0)
Else
S2 = Join( Ob.IPAddress, "," )
End If
End If
Exit For
Next
MsgBox S2
Set Col = Nothing
Set WMI = Nothing

DefaultIPGateway will return 192.168.1.1
Offhand I don't know whether there's a way to find 100.80.60.40.


--
--
"Dave "Crash" Dummy" <inv...@invalid.invalid> wrote in message
news:khfakv$5pj$1...@dont-email.me...

Ruediger Roesler

unread,
Mar 12, 2013, 10:00:13 AM3/12/13
to
Dave "Crash" Dummy <inv...@invalid.invalid> typed:

>I have a NAT router, and when I use ipconfig.exe, all I can see is the
> local LAN IP (192.18.1.xx). I can see the external WAN address in the
> router firmware (Tomato) GUI, but I don't know how to access that from
> script. I would like to read my external IP from a script. Any
> suggestions?
> Thanks!
>
> P.S. I am currently using a utility on a remote server to read the
> address. I want to keep everything "in house."

If your router is capable for UPnP you can use this script to determine
your public address, on the condition you have installed that protocoll
beneath Windows. But be careful:
http://en.wikipedia.org/wiki/Universal_Plug_and_Play#Problems_with_UPnP

*Millions of devices vulnerable via UPnP*:
http://www.h-online.com/security/news/item/Millions-of-devices-vulnerable-via-UPnP-Update-1794032.html

You can check your connection of vulnerabilities under this address:
http://upnp-check.rapid7.com/

<!--############################# XtIPA.wsf ################################-->
<package>
<job id="XtIP">
<object id="httpReq" progid="WinHttp.WinHttpRequest.5.1"/>
<resource id="autor"> from h.r.roesler</resource>
<runtime>
<description>
Script to determine the external (public) IP address via UPnP with
Windows XP SP3 or Windows 7 and any router fit for UPnP like some NAT-
Routers with newer design. It should be executed at the prompt with
CScript, as it is shown in the example.
</description>
<unnamed name="/?" helpstring="Help" many="false" required="false"/>
<example>CScript.exe XtIPA.wsf</example>
</runtime>
<script language="VBScript">
Option Explicit

Function HttpRequest(strCommand, strService, ByRef strOut)
Const URL = """http://schemas.xmlsoap.org/soap/envelope/"""
Const URN = """urn:schemas-upnp-org:service:WANIPConnection:1"""
Dim strReq, arrReq, i

strReq = "<?xml version=""1.0"" encoding=""utf-8""?>" & vbCRLF & _
"<s:Envelope xmlns:s=" & URL & " s:encodingStyle=" & _
Replace(URL, "envelope", "encoding") & ">" & vbCRLF & _
"<s:Body>" & vbCRLF & "<u:" & strCommand & " xmlns:u=" & _
URN & "/>" & vbCRLF & "</s:Body>" & vbCRLF & "</s:Envelope>"
arrReq = Array("Content-Type", "text/xml; charset=""utf-8""", _
"Connection", "close", "Content-Length", Len(strReq), _
"HOST", strHost & ":" & strPort, "SOAPACTION", _
Left(URN, Len(URN) - 1) & "#" & strCommand & """")
With httpReq
.Open "POST", strURL & "/upnp/control/" & strService, False
For i = 0 To UBound(arrReq) Step 2
.SetRequestHeader arrReq(i), arrReq(i + 1)
Next
.Send strReq
strOut = .ResponseText

HttpRequest = .Status = 200
End With
End Function

Function GetStatusInfo(ByRef strStatus, ByRef lngUpTime)
Dim strResponse, rgxMatch, isChanged, i

If HttpRequest("GetStatusInfo", "WANIPConn1", strResponse) Then
regX.Global = True
regX.Pattern = ">(.*)<"
For Each rgxMatch In regX.Execute(strResponse)
With rgxMatch
i = i + 1
If i = 1 Then
isChanged = strStatus <> .SubMatches(0)
If isChanged Then strStatus = .SubMatches(0)
If strStatus = "Disconnected" Then isChanged = False
ElseIf i = 2 And .SubMatches(0) <> "ERROR_NONE" Then
WScript.Echo "Fehler: " & .SubMatches(0)
isChanged = False: Exit For
ElseIf i = 3 And IsNumeric(.SubMatches(0)) Then
lngUpTime = CLng(.SubMatches(0))
If Not(isChanged) And lngUpTime = 0 Then
isChanged = lngUpTime = 0
End If
End If
End With
Next
regX.Global = False
End If

GetStatusInfo = isChanged
End Function

Sub GetExternalIPAddress()
Const WANCIFC1 = "WANCommonIFC1"
Dim strResult, lngSec, isValid

If GetStatusInfo(strResult, lngSec) And strResult = "Connected" Then
If HttpRequest("GetExternalIPAddress", WANCIFC1, strResult) Then
strResult = ValidateIPNum(strResult, False, isValid)
If isValid Then
WScript.Echo vbCRLF & "Since around " & DateAdd("s", _
lngSec * -1, Now) & " o'clock - " & strResult
Exit Sub
End If
End If
Else
WScript.Echo "At the moment no Internet connection exists." & vbCRLF
End If
WScript.Echo "The public IP address could not be determined."
End Sub

Function GetGateways(ByRef strQuery)
Dim wmiNIC

For Each wmiNIC In wmi.ExecQuery(strQuery)
With wmiNIC
If .IPEnabled Then
If Not(IsNull(.DefaultIPGateway)) Then
GetGateways = .DefaultIPGateway
End If
End If
End With
Next
End Function

Function Ping(strLANIP)
Const WQL = "select * from ", NIC = "Win32_NetworkAdapter", CONNECTED = 2
Const ASSOC = "Associators of {", ASSCLASS = "'} where AssocClass="
Const PIST = "Win32_PingStatus", IPA = "LAN-Address <", DID = ".DeviceID='"
Dim wmiPing, blnPong, strGW, wmiNIC, strWQL, strReq

strWQL = WQL & NIC & " where NetConnectionStatus = " & CONNECTED
For Each wmiNIC In wmi.ExecQuery(strWQL)
For Each strGW In GetGateways(ASSOC & NIC & DID & wmiNIC.DeviceID & _
ASSCLASS & NIC & "Setting")
strReq = WQL & PIST & " where ResolveAddressNames = true" & _
" and Address = '" & strGW & "'"
For Each wmiPing In wmi.ExecQuery(strReq)
With wmiPing
If IsNull(.StatusCode) Then Exit For
blnPong = .StatusCode = 0 And _
StrComp(strLANIP, .Address) = 0
If blnPong Then
WScript.Echo IPA & .ProtocolAddressResolved & _
">: " & .Address
End If
End With
Next
Next
If blnPong Then Exit For
Next
If Not(blnPong) Then
WScript.Echo "No corresponding network device with IP address <" & _
strLANIP & "> was recognized."
End If

Ping = blnPong
End Function

Function ValidateIPNum(ByRef strText, isPriv, ByRef isValid)
Dim strOktett, Match, intOkt, int1st, is2nd

regX.Pattern = "(?:\d{1,3}\.){3}(\d{1,3})"
If regX.Test(strText) Then
For Each Match In regX.Execute(strText)
For Each strOktett In Split(Match.Value, ".")
intOkt = CInt(strOktett)
If IsEmpty(int1st) Then
int1st = intOkt
isValid = int1st > 0
If isPriv Then
isValid = int1st = 10 Xor int1st = 172
isValid = isValid Xor int1st = 192
End If
ElseIf Not(is2nd) And isPriv Then
is2nd = True
Select Case int1st
Case 172: isValid = intOkt >= &H10 And intOkt < &H20
Case 192: isValid = intOkt = &HA8
End Select
End If
isValid = isValid And intOkt < &H100
If Not(isValid) Then Exit For
Next
If isPriv Then isValid = isValid And CInt(Match.SubMatches(0)) > 0
strOktett = Match.Value
Next
End If

ValidateIPNum = strOktett
End Function

Function GetURL(strText, ByRef isValid, ByRef strLANIPA, ByRef strPort)
Dim rgxMatch, strURL

With regX
.Pattern = "http:\/\/([^/:](?:\d{1,3}\.){3}\d{1,3}):([^:]\d{4,5})"
If .Test(strText) Then
For Each rgxMatch In .Execute(strText)
With rgxMatch
strURL = .Value
strLANIPA = ValidateIPNum(.SubMatches(0), True, isValid)
If isValid Then
If Ping(strLANIPA) Then
isValid = CLng(.SubMatches(1)) < &H10000
If isValid Then
strPort = .SubMatches(1)
WScript.Echo "Port number: " & strPort
End If
End If
End If
End With
Next
End If
End With

GetURL = strURL
End Function

Function FindDev(ByRef upnpDevice, ByRef strURL, ByRef strIPA, ByRef strPort)
Dim upnpDev, isURL, str, arr

For Each upnpDev In upnpDevice
With upnpDev
If .HasChildren Then
isURL = FindDev(.Children, strURL, strIPA, strPort)
End If
arr = Array(.ModelURL, .FriendlyName, .PresentationURL, .Type, _
.Description, .ManufacturerURL, .ManufacturerName, _
.UniqueDeviceName, .ModelName, .ModelNumber, .UPC)
End With
If Len(arr(1)) > 0 Then WScript.Echo vbCRLF & "Device: " & arr(1)
If Len(arr(2)) > 0 Then WScript.Echo "Available beneath <" & _
arr(2) & ">."
For Each str In arr
If Not(IsNull(str))Then
If Len(str) > 0 And Not(isURL) Then
strURL = GetURL(str, isURL, strIPA, strPort)
If isURL Then Exit For
End If
End If
Next
Next

FindDev = isURL
End Function

Const WINMGMTS = "winmgmts:{impersonationLevel=impersonate}!root/cimv2"
Const IGD = "urn:schemas-upnp-org:device:InternetGatewayDevice:1"
Dim wmi, regX, strURL, strHost, strPort

Set wmi = GetObject(WINMGMTS)
Set regX = New RegExp

WScript.Echo vbCRLF & vbTab & WScript.ScriptName & getResource("autor") & vbCRLF
On Error Resume Next
With CreateObject("UPnP.UPnPDeviceFinder")
If Err.Number <> 0 Then
WScript.Echo "The UPnP-Protocoll was not installed."
WScript.Quit
End If
WScript.Echo "One moment, searching for an Internet Gateway Device ..."
If Not(FindDev(.FindByType(IGD, 0), strURL, strHost, strPort)) Then
WScript.Echo "No Internet Gateway fit for UPnP was found."
WScript.Quit
End If
End With
On Error GoTo 0

WScript.Echo vbCRLF & "The external IP address is determined on " & _
FormatDateTime(Date, vbLongDate) & ":"
GetExternalIPAddress
</script>
</job>
</package>
<!-- This script was developed considering the *InGui4PowSavPro™*
(International Guidelines for Power-Saving Programming ;-)
################################## XtIPA.wsf ###############################-->

--
ЯR

Tom Del Rosso

unread,
May 29, 2013, 7:39:11 AM5/29/13
to

Dave "Crash" Dummy wrote:
> I have a NAT router, and when I use ipconfig.exe, all I can see is the
> local LAN IP (192.18.1.xx). I can see the external WAN address in the
> router firmware (Tomato) GUI, but I don't know how to access that from
> script. I would like to read my external IP from a script. Any
> suggestions?
> Thanks!
>
> P.S. I am currently using a utility on a remote server to read the
> address. I want to keep everything "in house."

Use wget to capture one of the popular IP-check websites.

For example, take the last line of text from this:

wget -qO- icanhazip.com

Other websites work but you have to parse more text.


--

Reply in group, but if emailing remove the last word.


Dave "Crash" Dummy

unread,
May 29, 2013, 12:03:49 PM5/29/13
to
That is what I have been doing. Depending on my mood, I use either
http://checkip.dyndns.org
or one I put on my own website, which fits nicely in an iframe without
parsing:
http://thedatalist.com/myIP.php

I would really like to do it without using an external server, though.
--
Crash

"The fewer the facts, the stronger the opinion."
~ Arnold H. Glasow ~

Tom Del Rosso

unread,
May 29, 2013, 1:54:33 PM5/29/13
to

Dave "Crash" Dummy wrote:
>
> That is what I have been doing. Depending on my mood, I use either
> http://checkip.dyndns.org
> or one I put on my own website, which fits nicely in an iframe without
> parsing:
> http://thedatalist.com/myIP.php
>
> I would really like to do it without using an external server, though.

I misunderstood, but since purely internal methods rely on the model of your
modem, and getting it from the modem is more complex, I don't see the
advantage.

If the ISP changes the firmware, it stops working. But if you grab it from
3 websites, you can distinguish a real IP change from a change on the sites.

Dave "Crash" Dummy

unread,
May 29, 2013, 5:59:11 PM5/29/13
to
Tom Del Rosso wrote:
> Dave "Crash" Dummy wrote:
>> That is what I have been doing. Depending on my mood, I use either
>> http://checkip.dyndns.org or one I put on my own website, which
>> fits nicely in an iframe without parsing:
>> http://thedatalist.com/myIP.php
>>
>> I would really like to do it without using an external server,
>> though.
>
> I misunderstood, but since purely internal methods rely on the model
> of your modem, and getting it from the modem is more complex, I don't
> see the advantage.

Just habit. I only recently added a router because I needed WiFi access
for my Kindle. Before I had the router, I could get the external IP with
ipconfig.exe.

The only real advantage is that external servers and/or pathways can crash.
0 new messages