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