Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for geoipdemo.vbs

(view source code of geoipdemo.vbs as plain text)

  1. Option Explicit
  2.  
  3. WScript.Echo Join( GeoIP( "www.amazon.com" ), ", " )
  4. WScript.Echo Join( GeoIP( "208.67.219.101" ), ", " )
  5.  
  6. Function GeoIP( myHost )
  7. 	' This function returns an array with the IP address, host name, city name,
  8. 	' country name and country code for a specified IP address or host name.
  9. 	'
  10. 	' Argument:
  11. 	' [string] IP address or host name
  12. 	'
  13. 	' Returns:
  14. 	' [array of string] host name, IP address, city, country, country code
  15. 	'
  16. 	' Required software:
  17. 	' MaxMind's GeoIP* database:   http://www.maxmind.com/app/installation?city=1
  18. 	' MaxMind's GeoIP COM wrapper: http://www.maxmind.com/app/com
  19. 	' System Scripting Runtime:    http://www.netal.com/ssr.htm
  20. 	'
  21. 	' Written by Rob van der Woude
  22. 	' http://www.robvanderwoude.com
  23.  
  24. 	Dim objGeoIP, objDNS, objRE, strDNS, strHost, strIP
  25.  
  26. 	Set objDNS   = CreateObject( "SScripting.IPNetwork" )
  27. 	Set objGeoIP = CreateObject( "GeoIPCOMEx.GeoIPEx" )
  28. 	Set objRE    = New RegExp
  29.  
  30. 	' Resolve the specified host name or IP address
  31. 	strDNS = objDNS.DNSLookup( myHost )
  32.  
  33. 	' Check if the argument is a host name or an IP address
  34. 	objRE.Pattern = "^[1-9][0-9]{0,2}(\.[0-9]{1,2}){3}$"
  35. 	If objRE.Test( myHost ) Then
  36. 		strIP   = myHost
  37. 		strHost = strDNS
  38. 	Else
  39. 		strIP   = strDNS
  40. 		strHost = myHost
  41. 	End If
  42.  
  43. 	With objGeoIP
  44. 		' Modify the path if you installed the GeoIP* database elsewhere
  45. 		.set_db_path "C:\Program Files\GeoIP\"
  46. 		.find_by_addr strIP
  47. 		GeoIP = Array( strHost, strIP, .city, .country_name, .country_code )
  48. 	End With
  49.  
  50. 	Set objDNS   = Nothing
  51. 	Set objGeoIP = Nothing
  52. 	Set objRE    = Nothing
  53. End Function
  54.  

page last modified: 2024-04-16; loaded in 0.0059 seconds