PUBLISH YOUR HOME IP ADDRESS TO YOUR WEB SERVER
WITH XMLHTTP

By Peter A. Bromberg, Ph.D.

Peter Bromberg  


if you're connected via cablemodem at home, and you often need a way to determine what your home PC's IP address is, then this little IP - publishing trick will save you a lot of time. The key to doing this is to use the WMI Locator object. All you need to do is create a VBS file that grabs your IP's off your NIC and using XMLHTTP, sends them out to a special receiver page on your webserver, which simply writes the results to a little HTM file that you can visit over the web from anywhere. Then if you have VNC, PCAnywhere or Terminal Services running on your home PC, you'll know how to reach it. The VBS script can be set to run under the NT Scheduling Service each hour, for example so that if there are any DHCP changes, they'll be updated to your public webserver.



First, here's some sample code for the VBS file:


Dim arIps
dim machinename
set ws = CreateObject("Wscript.network")
machinename=ws.computername
set ws = Nothing
arIPs = GetIPAddress(machinename, "", "")
arIPs = Replace (arIPs,":", "")
dim xmlhttp
set xmlhttp =CreateObject("MSXML2.XMLHTTP.3.0")
xmlhttp.Open "POST", "http://www.yourserver.com/ReceiveIP.asp",false
xmlhttp.send arIPs
set xmlhttp =nothing
set objLocator=Nothing
set objService=Nothing
wscript.echo arIPs & " for " & machinename & " sent!"

function GetIPAddress (server, username, password)
set objLocator = CreateObject("WbemScripting.SWbemLocator")
set objService = objLocator.ConnectServer (server, "", username, password)
strQuery = "Select IPAddress from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE"
set objEnumerator = objService.ExecQuery(strQuery)
for each obj in objEnumerator
if Not IsNull(obj.IPAddress) then
for i=LBound(obj.IPAddress) to UBound(obj.IPAddress)
GetIPaddress =GetIPAddress & ": " & obj.IPAddress(i)
next
end if
next
end function

What we are doing here is:

1. Create a Wscript.network object to dynamically get the machine name (makes the script more portable)
2. Pass the machine name to the "GetIPAddress" function. Note that for a local machine you can set the username and password to null strings.
3. GetIPAddress creates an instance of the WbemScripting.SWbemLocator object, and calls the query to get all active IP's from the NIC
4. We then iterate through the collection and create a semicolon-delimited flat array out of the results, suitable for sending over the wire.
5. This result is sent up to your server using XMLHTTP, and lastly, we just echo out a little message showing it worked. You could also use a timed popup here.

Now let's switch over to our webserver where our "ReceiveIP.asp" page is waiting:

<%
sdata = Request.BinaryRead(Request.TotalBytes)
sdata = BinaryToString(sdata)
Response.binaryWrite sdata
Set fstemp = server.CreateObject("Scripting.FileSystemObject")
Set filetemp = fstemp.CreateTextFile(server.mappath(".") & "\myipaddresses.htm", true)
' true = file can be over-written if it exists
' false = file CANNOT be over-written if it exists
filetemp.write( sdata)
filetemp.Close
Function BinaryToString(Binary)
Dim cl1, cl2, cl3, pl1, pl2, pl3
Dim L
Dim tstChar
cl1 = 1
cl2 = 1
cl3 = 1
L = LenB(Binary)
Do While cl1<=L
tstChar=Chr(AscB(MidB(Binary,cl1,1)))
pl3 = pl3 & tstChar
cl1 = cl1 + 1
cl3 = cl3 + 1
If cl3>300 Then
pl2 = pl2 & pl3
pl3 = ""
cl3 = 1
cl2 = cl2 + 1
If cl2>200 Then
pl1 = pl1 & pl2
pl2 = ""
cl2 = 1
End If
End If
Loop
BinaryToString = pl1 & pl2 & pl3
End function
%>

Now at the server,
1) we do a binaryread of the TotalBytes from the Request object to get the entire XMLHTTP post. We could just as easily use another method, but I thought it would be fun to do a binary - to - string conversion and not use any COM objects except to write the file.
2) We convert the byte array in "sdata" to a string by passing it to the BinaryToString function (this is an optimized version).
3) We create the file "myipaddresses.htm" which will contain nothing more than our semicolon -delimited list of IP addresses, and write the file.
4) VOILA! You just published your home IP address to your webserver!

Stay tuned. For my next trick I'll show you how to get through the firewall at work with VNC to get onto the desktop of your home PC! Firewall administrators, cringe!

 

Peter Bromberg is an independent consultant specializing in distributed .NET solutionsa Senior Programmer /Analyst at in Orlando and a co-developer of the NullSkull.com developer website. He can be reached at info@eggheadcafe.com