Jump to content

Faster, shorter, _GetIp()


TehWhale
 Share

Recommended Posts

Hardly any faster.

#include <INet.au3>

$init = TimerInit()
For $n = 0 to 10
    _GetIP()
Next
MsgBox(0, "1", TimerDiff($init))

$init = TimerInit()
For $n = 0 to 10
    _GetIP2()
Next
MsgBox(0, "2", TimerDiff($init))

Func _GetIP2()
    Return _INetGetSource("whatismyip.org")
EndFunc

_GetIp loop takes a constant 2500ms to perform in total.

_GetIp2 loop ranges between 2200 to 3300.

Edited by Manadar
Link to comment
Share on other sites

Hardly any faster.

#include <INet.au3>

$init = TimerInit()
For $n = 0 to 10
    _GetIP()
Next
MsgBox(0, "1", TimerDiff($init))

$init = TimerInit()
For $n = 0 to 10
    _GetIP2()
Next
MsgBox(0, "2", TimerDiff($init))

Func _GetIP2()
    Return _INetGetSource("whatismyip.org")
EndFunc

_GetIp loop takes a constant 2500ms to perform in total.

_GetIp2 loop ranges between 2200 to 3300.

I did that, and got around 2500 for getIp, and for #2 i got around 2000, but i guess its because of internet connections, and the websites ping.
Link to comment
Share on other sites

Here's a thought.

I think http://whatismyip.org is the best website for retrieving your IP address because it doesn't have a bunch of useless crap! It gives your external IP address, that's it!. Now if they had servers all over the globe and deleted their server logs this could be a standard website for retrieving your IP address, no more hassle with crappy websites nearest to your country. :)

Don't bother, It's inside your monitor!------GUISetOnEvent should behave more like HotKeySet()
Link to comment
Share on other sites

Here's a thought.

I think http://whatismyip.org is the best website for retrieving your IP address because it doesn't have a bunch of useless crap! It gives your external IP address, that's it!. Now if they had servers all over the globe and deleted their server logs this could be a standard website for retrieving your IP address, no more hassle with crappy websites nearest to your country. :)

Func _GetIP()
    Local $ip, $t_ip
    If InetGet("http://checkip.dyndns.org/?rnd1=" & Random(1, 65536) & "&rnd2=" & Random(1, 65536), @TempDir & "\~ip.tmp") Then
        $ip = FileRead(@TempDir & "\~ip.tmp", FileGetSize(@TempDir & "\~ip.tmp"))
        FileDelete(@TempDir & "\~ip.tmp")
        $ip = StringTrimLeft($ip, StringInStr($ip, ":") + 1)
        $ip = StringTrimRight($ip, StringLen($ip) - StringInStr($ip, "/") + 2)
        $t_ip = StringSplit($ip, '.')
        If $t_ip[0] = 4 And StringIsDigit($t_ip[1]) And StringIsDigit($t_ip[2]) And StringIsDigit($t_ip[3]) And StringIsDigit($t_ip[4]) Then
            Return $ip
        EndIf
    EndIf
    If InetGet("http://www.whatismyip.com/?rnd1=" & Random(1, 65536) & "&rnd2=" & Random(1, 65536), @TempDir & "\~ip.tmp") Then
        $ip = FileRead(@TempDir & "\~ip.tmp", FileGetSize(@TempDir & "\~ip.tmp"))
        FileDelete(@TempDir & "\~ip.tmp")
        $ip = StringTrimLeft($ip, StringInStr($ip, "Your ip is") + 10)
        $ip = StringLeft($ip, StringInStr($ip, " ") - 1)
        $ip = StringStripWS($ip, 8)
        $t_ip = StringSplit($ip, '.')
        If $t_ip[0] = 4 And StringIsDigit($t_ip[1]) And StringIsDigit($t_ip[2]) And StringIsDigit($t_ip[3]) And StringIsDigit($t_ip[4]) Then
            Return $ip
        EndIf
    EndIf
    SetError(1)
    Return -1
EndFunc   ;==>_GetIP
Link to comment
Share on other sites

func getIp()
    tcpStartup()
    Local $ip = TCPNameToIP("www.whatismyip.com")
    local $sock = tcpConnect($ip, 80)
    
    if $sock = -1 then
        tcpShutdown()
        return 0
    Else
        
        local $pPacket = "GET /automation/n09230945.asp HTTP/1.1"&@CRLF
              $pPacket &= "Host: www.whatismyip.com"&@CRLF
              $pPacket &= "User-Agent: AutoIt/1.0" & @CRLF
              $pPacket &= "Connection: close"&@CRLF&@CRLF
              
        tcpSend($sock, $pPacket)
        local $recv, $ret, $timeout = timerInit()
        do
            
            $recv = tcpRecv($sock, 512)
            if (@error OR timerDiff($timeout) >= 5000) then 
                tcpCloseSocket($sock)
                tcpShutdown()
                return 0
            EndIf
    
        until $recv

        $recv = stringSplit($recv, @CRLF&@CRLF, 1)
        if $recv[0] == 2 Then
            $ret = $recv[2]
        Else
            $ret = 0
        EndIf
        tcpCloseSocket($sock)
        tcpShutdown()
        return $ret
    EndIf
EndFunc

~300 ms ;-)

Link to comment
Share on other sites

func getIp()
    tcpStartup()
    Local $ip = TCPNameToIP("www.whatismyip.com")
    local $sock = tcpConnect($ip, 80)
    
    if $sock = -1 then
        tcpShutdown()
        return 0
    Else
        
        local $pPacket = "GET /automation/n09230945.asp HTTP/1.1"&@CRLF
              $pPacket &= "Host: www.whatismyip.com"&@CRLF
              $pPacket &= "User-Agent: AutoIt/1.0" & @CRLF
              $pPacket &= "Connection: close"&@CRLF&@CRLF
              
        tcpSend($sock, $pPacket)
        local $recv, $ret, $timeout = timerInit()
        do
            
            $recv = tcpRecv($sock, 512)
            if (@error OR timerDiff($timeout) >= 5000) then 
                tcpCloseSocket($sock)
                tcpShutdown()
                return 0
            EndIf
    
        until $recv

        $recv = stringSplit($recv, @CRLF&@CRLF, 1)
        if $recv[0] == 2 Then
            $ret = $recv[2]
        Else
            $ret = 0
        EndIf
        tcpCloseSocket($sock)
        tcpShutdown()
        return $ret
    EndIf
EndFunc

~300 ms ;-)

Congrats. That is VERY good.
Link to comment
Share on other sites

Func _GetIP()
    Local $ip, $t_ip
    If InetGet("http://checkip.dyndns.org/?rnd1=" & Random(1, 65536) & "&rnd2=" & Random(1, 65536), @TempDir & "\~ip.tmp") Then
        $ip = FileRead(@TempDir & "\~ip.tmp", FileGetSize(@TempDir & "\~ip.tmp"))
        FileDelete(@TempDir & "\~ip.tmp")
        $ip = StringTrimLeft($ip, StringInStr($ip, ":") + 1)
        $ip = StringTrimRight($ip, StringLen($ip) - StringInStr($ip, "/") + 2)
        $t_ip = StringSplit($ip, '.')
        If $t_ip[0] = 4 And StringIsDigit($t_ip[1]) And StringIsDigit($t_ip[2]) And StringIsDigit($t_ip[3]) And StringIsDigit($t_ip[4]) Then
            Return $ip
        EndIf
    EndIf
    If InetGet("http://www.whatismyip.com/?rnd1=" & Random(1, 65536) & "&rnd2=" & Random(1, 65536), @TempDir & "\~ip.tmp") Then
        $ip = FileRead(@TempDir & "\~ip.tmp", FileGetSize(@TempDir & "\~ip.tmp"))
        FileDelete(@TempDir & "\~ip.tmp")
        $ip = StringTrimLeft($ip, StringInStr($ip, "Your ip is") + 10)
        $ip = StringLeft($ip, StringInStr($ip, " ") - 1)
        $ip = StringStripWS($ip, 8)
        $t_ip = StringSplit($ip, '.')
        If $t_ip[0] = 4 And StringIsDigit($t_ip[1]) And StringIsDigit($t_ip[2]) And StringIsDigit($t_ip[3]) And StringIsDigit($t_ip[4]) Then
            Return $ip
        EndIf
    EndIf
    SetError(1)
    Return -1
EndFunc   ;==>_GetIP
What does that have to do with what I just said? :)
Don't bother, It's inside your monitor!------GUISetOnEvent should behave more like HotKeySet()
Link to comment
Share on other sites

What does that have to do with what I just said? :)

It's the standard _GetIp function in AutoIt and it uses www.whatismyip.org, like you said. If you can't understand the link between those, then you're retarded.

~300 ms ;-)

I wrote this earlier in the topic, but decided not to share because it's useless.

TCPStartup()

ConsoleWrite( _GetIP() & @CRLF) ;; to see if it works

$init = TimerInit()
For $n = 0 to 9 
    _GetIP()
Next
$diff = TimerDiff($init)

MsgBox(0, "", $diff)


func _GetIp()
    Local $sTargetAddress = TCPNameToIP("www.whatismyip.com")
    If @error Then
        Return SetError(1,0,"0.0.0.0")
    EndIf
    
    local $hSocket = TCPConnect($sTargetAddress, 80)
    If @error Then
        Return SetError(2,0,"0.0.0.0")
    EndIf
        
    Local $sGETPacket = "GET /automation/n09230945.asp HTTP/1.1" & @LF & "Host: www.whatismyip.com" & @LF & "Connection: close"&@LF&@LF
    Local $sReturnPacket 
    
    TCPSend($hSocket,$sGETPacket)
    If @error Then
        Return SetError(3,0,"0.0.0.0")
    EndIf
    
    While 1
        $sReturnPacket &= TcpRecv($hSocket, 512)
        If @error Then
            TCPCloseSocket($hSocket)
            ExitLoop
        EndIf
    WEnd
    
    $aParsed = StringSplit(StringStripCR($sReturnPacket),@LF&@LF,1)
    If $aParsed[0] == 2 Then
        Return $aParsed[2]
    Else
        Return SetError(4,0,"0.0.0.0")
    EndIf
EndFunc
Edited by Manadar
Link to comment
Share on other sites

  • Moderators

This was sporadic:

Local $s_ip = ""
Local $i_timer = TimerInit()
For $i = 1 To 10
    $s_ip = _WhatsMyIP()
Next
Local $i_diff = TimerDiff($i_timer)
ConsoleWrite("It took: " & Round($i_diff / 1000, 2) & " seconds and your IP is: " & $s_ip & @CRLF)


Func _WhatsMyIP()
    Local $s_val = "", $s_ret
    Local $i_pid = Run(@ComSpec & " /c tracert.exe http://www.google.com -h 1", @SystemDir, @SW_HIDE, 6)
    While 1
        $s_val &= StdoutRead($i_pid)
        If @error Then ExitLoop
        $s_ret = StringRegExpReplace($s_val, "(?s)(?:.*?\[.*?\].*?\[(.*?)\].*?\z)", "\1")
        If @extended Then ExitLoop
    WEnd
    ProcessClose($i_pid)
    Return $s_ret
EndFunc

Edit:

Changed to standard code tags.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

It took: 0.62 seconds and your IP is: Unable to resolve target system name http://www.google.com.

Also, if you try to use a single hop then it's going to fail on my machine for sure. To get my WAN ip it needs at least 4 hops.

Not so hard a fix if you just add 10 to the end of the hop and get rid of the for/next loop. However it's probably going to take longer.

Local $s_ip = ""
Local $i_timer = TimerInit()
$s_ip = _WhatsMyIP()
Local $i_diff = TimerDiff($i_timer)
ConsoleWrite("It took: " & Round($i_diff / 1000, 2) & " seconds and your IP is: " & $s_ip & @CRLF)


Func _WhatsMyIP()
    Local $s_val = "", $s_ret
    Local $i_pid = Run(@ComSpec & " /c tracert.exe http://www.google.com -h 10", @SystemDir, @SW_HIDE, 6)
    While 1
        $s_val &= StdoutRead($i_pid)
        If @error Then ExitLoop
        $s_ret = StringRegExpReplace($s_val, "(?s)(?:.*?\[.*?\].*?\[(.*?)\].*?\z)", "\1")
        If @extended Then ExitLoop
    WEnd
    If @extended = 0 Then $s_ret = ""
    ProcessClose($i_pid)
    Return $s_ret
EndFunc
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Not so hard a fix if you just add 10 to the end of the hop and get rid of the for/next loop. However it's probably going to take longer.

In any case, the method will never be good enough to be considered as a release candidate. It will either bug out a lot, tracert will be missing, or it will be too slow.

I really appreciate your effort, though. Just because something seem like the perfect solution, doesn't mean we can't try it and come up with unexpected answers. :)

Link to comment
Share on other sites

  • Moderators

In any case, the method will never be good enough to be considered as a release candidate. It will either bug out a lot, tracert will be missing, or it will be too slow.

I really appreciate your effort, though. Just because something seem like the perfect solution, doesn't mean we can't try it and come up with unexpected answers. :)

It's surely not the perfect solution, and I know I expressed the "sporadic" results in the first post... just thought I'd throw in ideas like you suggested above. It was something I used for myself a while back because I knew how it was going to react.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...