VBScript & SQL: Programatically Find the Location of an IP Address
This is more of a post for proper keywords. My previous entry titled "Import MaxMind City CSVs into SQL Server 2005" assumes the user has already conducted research to find that MaxMind's free GeoIP database provides the functionality to look up the location of an IP address. So, if you wish to quickly and easily find an IP address' location, do the following.
1. Ensure you have SQL Server installed somewhere. If you don't currently have it installed, you can download SQL Server 2005 Express for free. 2. Read this post about importing the necessary (and free) database for IP lookups. a. Download zip & unzip b. Read README.txt c. Copy the unzip.exe to your Windows directory d. Run the included SQL File e. Run the included VBS file 3. Create a VBS file to access the information. 4. Double-click the VBS file and enter the IP address
1strSQLServer = "localhost"
2strDBName = "maxmindGeoIP"
3
4strIPaddr = InputBox("Enter the IP address:", "IP To Location")
5
6If Len(strIPaddr) = 0 Then
7 Wscript.Quit
8End If
9
10Set objRS = CreateObject("ADODB.recordset")
11strConnstring = "Driver={SQL Server};Server=" & strSQLServer & ";Database=" & strDBName & ";Trusted_Connection=Yes;"
12strSQL = "EXEC usp_IPtoLocation '" & strIPaddr & "'"
13objRS.Open strSQL, strConnstring, 1, 1
14
15If objRS.EOF And objRS.BOF Then
16 MsgBox "Something is ultra-broken. Is your database populated? " 'Everything should return at least something.
17Else
18 strLocation = objRS("location")
19 If InStr(strLocation, "Unknown") > 0 Then
20 MsgBox "The IP address, " & strIPaddr & ", cannot be found in the database :(", 64, "IP To Location"
21 Else
22 MsgBox strIPaddr & " resolves to.." & vbCrLf & vbCrLf & objRS("location"), 64, "IP To Location"
23 End If
24End If
25
26Set objRS = Nothing
Then, finding the location is as easy as double-clicking the file, entering an IP, and pressing "OK"....

In less than 1 millisecond, (after the initial stored procedure execution), your results will show up
