Here is my version
GetExternalIP()
{
/* --- used sites in order of call
http://www.whatismyip.org/
http://checkip.dyndns.com/
http://www.formyip.com/
*/
; 1: ip only
URL := "http://www.whatismyip.org/"
If ( InternetFileRead( ipsrc, URL, 100, 100, "No-Progress" ) > 0 )
{
If IsValidIP(ipsrc)
Return ipsrc
}
; 2: <body>Current IP Address: xxx.xxx.xxx.xxx</body>
URL := "http://checkip.dyndns.com/"
If ( InternetFileRead( ipsrc, URL, 100, 100, "No-Progress" ) > 0 )
{
someip := StringBetween(ipsrc, "IP Address: ", "<")
If IsValidIP(someip)
Return someip
}
; 3: <title>My ip address is xxx.xxx.xxx.xxx The
URL := "http://www.formyip.com/"
If ( InternetFileRead( ipsrc, URL, 100, 100, "No-Progress" ) > 0 )
{
someip := StringBetween(ipsrc, "My ip address is ", " The")
If IsValidIP(someip)
Return someip
}
; fall through
Return "Error"
}
StringBetween(string, before, after)
{
fp := RegExMatch(string, "i)\Q" . before . "\E(.*?)\Q" . after . "\E", sb)
Return sb1
}
; SKAN: http://www.autohotkey.com/forum/post-178165.html#178165
IsValidIP(IP)
{
Return RegExMatch( IP, "^(25[0-5]|2[0-4][0-9]|[01]?[0-9]"
. "[0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
. "\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9"
. "]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$" )
}
; SKAN: http://www.autohotkey.com/forum/topic45718.html
InternetFileRead( ByRef V, URL="", RB=0, bSz=1024, DLP="DLP", F=0x84000000 ) {
Static LIB="WININET\", QRL=16, CL="00000000000000", N=""
If ! DllCall( "GetModuleHandle", Str,"wininet.dll" )
DllCall( "LoadLibrary", Str,"wininet.dll" )
If ! hIO:=DllCall( LIB "InternetOpenA", Str,N, UInt,4, Str,N, Str,N, UInt,0 )
Return -1
If ! (( hIU:=DllCall( LIB "InternetOpenUrlA", UInt,hIO, Str,URL, Str,N, Int,0, UInt,F
, UInt,0 ) ) || ErrorLevel )
Return 0 - ( !DllCall( LIB "InternetCloseHandle", UInt,hIO ) ) - 2
If ! ( RB )
If ( SubStr(URL,1,4) = "ftp:" )
CL := DllCall( LIB "FtpGetFileSize", UInt,hIU, UIntP,0 )
Else If ! DllCall( LIB "HttpQueryInfoA", UInt,hIU, Int,5, Str,CL, UIntP,QRL, UInt,0 )
Return 0 - ( !DllCall( LIB "InternetCloseHandle", UInt,hIU ) )
- ( !DllCall( LIB "InternetCloseHandle", UInt,hIO ) ) - 4
VarSetCapacity( V,64 ), VarSetCapacity( V,0 )
SplitPath, URL, FN,,,, DN
FN:=(FN ? FN : DN), CL:=(RB ? RB : CL), VarSetCapacity( V,CL,32 ), P:=&V,
B:=(bSz>CL ? CL : bSz), TtlB:=0, LP := RB ? "Unknown" : CL, %DLP%( True,CL,FN )
Loop {
If ( DllCall( LIB "InternetReadFile", UInt,hIU, UInt,P, UInt,B, UIntP,R ) && !R )
Break
P:=(P+R), TtlB:=(TtlB+R), RemB:=(CL-TtlB), B:=(RemB<B ? RemB : B), %DLP%( TtlB,LP )
Sleep -1
} TtlB<>CL ? VarSetCapacity( T,TtlB ) DllCall( "RtlMoveMemory", Str,T, Str,V, UInt,TtlB )
. VarSetCapacity( V,0 ) . VarSetCapacity( V,TtlB,32 ) . DllCall( "RtlMoveMemory", Str,V
, Str,T, UInt,TtlB ) . %DLP%( TtlB, TtlB ) : N
If ( !DllCall( LIB "InternetCloseHandle", UInt,hIU ) )
+ ( !DllCall( LIB "InternetCloseHandle", UInt,hIO ) )
Return -6
Return, VarSetCapacity(V)+((ErrorLevel:=(RB>0 && TtlB<RB)||(RB=0 && TtlB=CL) ? 0 : 1)<<64)
}
HTH