Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

[SOLVED] Determine country from IP address


  • Please log in to reply
6 replies to this topic

#1 Juanola

Juanola

    Seeker

  • Active Members
  • 47 posts

Posted 08 February 2009 - 10:44 AM

I would like do one tool with Autoit to determine the country by IP address.

Somebody could help me?

Thank you.

Edited by Juanola, 17 February 2009 - 06:41 PM.








#2 furrycow

furrycow

    Prodigy

  • Active Members
  • PipPipPip
  • 177 posts

Posted 08 February 2009 - 10:50 AM

I would like do one tool with Autoit to determine the country by IP address.

Somebody could help me?

Thank you.


Youll need to use a database full of all the values, there is this one which is a free database:

http://ip-to-country.webhosting.info/downl...country.csv.zip

I think all the values in there are recorded in decimal, so you would have to convert an ip address to a decimal value before comparing it to see where it came from.

EDIT:

This shows you how to convert it...

http://ip-to-country.webhosting.info/node/view/55

Edited by furrycow, 08 February 2009 - 10:52 AM.

Instant Lockerz Invite - www.instantlockerzinvite.co.uk

#3 Juanola

Juanola

    Seeker

  • Active Members
  • 47 posts

Posted 08 February 2009 - 06:21 PM

Thank you!!!

I have done it script but it is very slow searching the string in the file. The file have one size of 2 Mb.


Any help?


AutoIt         
#include <file.au3> dim $array_num $file="ip.txt" $num_decimal=0 $ip="0" $ip=InputBox("Country IP","Number IP","","",80,120,-1,-1) if ($ip<>"") Then         $array_num=stringsplit($ip,".")     $num_decimal=($array_num[1] * 16777216) + ($array_num[2] * 65536) + ($array_num[3] * 256) + $array_num[4]         $country_find=False     if ($num_decimal<>0) Then             $file_read=FileOpen($file,0)             while 1 or $country_find=False                 $line=FileReadLine($file_read)                 If @error = -1 Then ExitLoop                                 if(StringInStr($line,$num_decimal)) Then                     $country_find=True                     $country_array=StringSplit($line,",")                     $country=StringMid($country_array[$country_array[0]],2,stringlen($country_array[$country_array[0]])-2)                 EndIf               WEnd                         fileclose($file_read)             if ($country_find=False) Then                 msgbox(16,"Error","IP Unknown")             Else                 msgbox(16,"Country IP","The IP " & $ip & " is from " & $country)                EndIf     EndIf EndIf


Thank you!

Edited by Juanola, 08 February 2009 - 10:19 PM.


#4 martin

martin

    ~~\o/~~~/0\=¬''~~~

  • MVPs
  • 7,321 posts

Posted 08 February 2009 - 06:40 PM

Thank you!!!

I have done it script but it doesn't working.

The num_decimal never is found in the file "ip.txt" and it is very slow searching the string in the file. The file have one size of 2 Mb.


The formula to convert an IP Address of the form A.B.C.D to an IP Number is:
Number = A x (256*256*256) + B x (256*256) + C x 256 + D


Is it Formula OK for find the country of one IP?

Any help?


AutoIt         
#include <file.au3> dim $array_num $file="ip.txt" $num_decimal=0 $ip="0" $ip=InputBox("Country IP","Number IP","","",80,120,-1,-1) if ($ip<>"") Then         $array_num=stringsplit($ip,".")     $num_decimal=$array_num[1] * 16777216 + $array_num[2] * 65536 + $array_num[3] * 256 + $array_num[4]         $country_find=False     if ($num_decimal<>0) Then             $file_read=FileOpen($file,0)             while 1 or $country_find=False                 $line=FileReadLine($file_read)                 If @error = -1 Then ExitLoop                                 if(StringInStr($line,$num_decimal)) Then                     $country_find=True                     $country_array=StringSplit($line,",")                     $country=StringMid($country_array[$country_array[0]],2,stringlen($country_array[$country_array[0]])-2)                 EndIf               WEnd                         fileclose($file_read)             if ($country_find=False) Then                 msgbox(16,"Error","IP Unknown")             Else                 msgbox(16,"Country IP","The IP " & $ip & " is from " & $country)                 EndIf     EndIf EndIf


Thank you!

Maybe read the whole file
$Alltext = FileRead($file)

Then search $Alltext for the code you want.
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

#5 Juanola

Juanola

    Seeker

  • Active Members
  • 47 posts

Posted 17 February 2009 - 05:29 AM

Somebody could test my script????

I think what it isn't working OK, because all the IP's what i test always give result UNKNOWN.

Please HELP ME.

Thank you!

Edited by Juanola, 17 February 2009 - 08:00 AM.


#6 Authenticity

Authenticity

    Universalist

  • MVPs
  • 2,619 posts

Posted 17 February 2009 - 09:19 AM

AutoIt         
Opt('MustDeclareVars', 1) Dim $sFile = @ScriptDir & '\ip-to-country.csv' Dim $iIPnum Dim $sIP, $sCSV Dim $sCountry, $sccTLD, $sAbbrev; country code top-level domain Dim $aRes, $aIP Dim $sMinIP, $sMaxIP Dim $i Dim $Init = TimerInit() $sCSV = FileRead($sFile) ;$aRes = StringRegExp($sCSV, '(?:"(\d+)","(\d+)","([A-Z]{2})","([A-Z]{3})","([A-Z ]++)"\n)', 3) $aRes = StringRegExp($sCSV, '"(\d+)","(\d+)"', 3) Do     $sIP = InputBox('IP address', 'Please enter your IP address', '181.97.133.4') Until Not @error $aIP = stringsplit($sIP, ".") If Not IsArray($aIP) Or UBound($aIP) < 5 Then Exit $iIPnum = $aIP[1] * 16777216 + $aIP[2] * 65536 + $aIP[3] * 256 + $aIP[4] For $i = 0 To UBound($aRes)-1 Step 2 ;5     If $iIPnum >= $aRes[$i] And $iIPnum <= $aRes[$i+1] Then         $sMinIP = $aRes[$i]         $sMaxIP = $aRes[$i+1]         ;$sccTLD = $aRes[$i+2]         ;$sAbbrev = $aRes[$i+3]         ;$sCountry = $aRes[$i+4]         ExitLoop     EndIf Next $aRes = StringRegExp($sCSV, '(?:"' & $sMinIP & '","' & $sMaxIP & '","([A-Z]{2})","([A-Z]{3})","([A-Z ]+)"\n)', 3) If Not IsArray($aRes) Then     MsgBox(0x10, 'Error', 'The IP address is not valid')     Exit EndIf ;$sccTLD = $aRes[$i+2] ;$sAbbrev = $aRes[$i+3] ;$sCountry = StringLower($aRes[$i+4]) $sccTLD = $aRes[0] $sAbbrev = $aRes[1] $sCountry = StringLower($aRes[2]) Dim $sText = 'Your Internet country code top-level domain is: ' & $sccTLD & _      @CRLF & 'Your country abbreviation is: ' & $sAbbrev & _      @CRLF & 'So you live in: ' & $sCountry ConsoleWrite(TimerDiff($Init) & @CRLF) MsgBox(0x40, 'Info.', $sText)


I've commented a few parts of the code that the initial load takes ~3 seconds more than the current one but once loaded it's much faster than the current code because it doesn't involve another StringRegExp call.

Edited by Authenticity, 17 February 2009 - 09:23 AM.


#7 Juanola

Juanola

    Seeker

  • Active Members
  • 47 posts

Posted 17 February 2009 - 05:30 PM

Thank you very much!!!
Your script is very fast and working OK!!!

You are one Authentic Master!!!

I'm learning very with you!!!

Thank you!!!!!!!

Edited by Juanola, 17 February 2009 - 05:30 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users