Sign in to follow this  
Followers 0

[SOLVED] Determine country from IP address

7 posts in this topic

Posted (edited)

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

Somebody could help me?

Thank you.

Edited by Juanola

Share this post


Link to post
Share on other sites



Posted (edited)

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

Share this post


Link to post
Share on other sites

Posted (edited)

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?

#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

Share this post


Link to post
Share on other sites

Posted

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?

#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.

Share this post


Link to post
Share on other sites

Posted (edited)

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

Share this post


Link to post
Share on other sites

Posted (edited)

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

Share this post


Link to post
Share on other sites

Posted (edited)

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0