AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Verify an IP Address

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
PentePirate



Joined: 26 Jul 2007
Posts: 1

PostPosted: Thu Jul 26, 2007 3:56 am    Post subject: Verify an IP Address Reply with quote

This is a small function to verify that a string is a valid IP address.

Code:

ValidIP(ByRef IPAddress)
{
   if (StrLen(IPAddress) > 15)
      Valid=0

   IfInString, IPAddress, %A_Space%
      Valid=0

   StringSplit, Octets, IPAddress, .
   if (Octets0 <> 4)
      Valid=0
   else if (Octets1 < 1 || Octets1 > 255)
      Valid=0
   else if (Octets2 < 0 || Octets2 > 255)
      Valid=0
   else if (Octets3 < 0 || Octets3 > 255)
      Valid=0
   else if (Octets4 < 0 || Octets4 > 255)
      Valid=0
   else Valid=1

   Oct1:=Octets1*0
   Oct2:=Octets2*0
   Oct3:=Octets3*0
   Oct4:=Octets4*0

   if (Oct1 <> 0 || Oct2 <> 0 || Oct3 <> 0 || Oct4 <> 0)
      Valid=0

   return %Valid%
}


Hope you find it useful...

PentePirate
Back to top
View user's profile Send private message Send e-mail
OctalMage as a guest
Guest





PostPosted: Thu Jul 26, 2007 4:22 am    Post subject: thanks Reply with quote

Wow works great, here it my test code.

Code:

ip=10.10.10.10
result:=ValidIP(ip)
msgbox %result% ;returns 1

ip=10.10.10.1a
result:=ValidIP(ip)
msgbox %result% ;returns 0

ip=10.10.10.600
result:=ValidIP(ip)
msgbox %result% ;returns 0



return
ValidIP(ByRef IPAddress)
{
   if (StrLen(IPAddress) > 15)
      Valid=0

   IfInString, IPAddress, %A_Space%
      Valid=0

   StringSplit, Octets, IPAddress, .
   if (Octets0 <> 4)
      Valid=0
   else if (Octets1 < 1 || Octets1 > 255)
      Valid=0
   else if (Octets2 < 0 || Octets2 > 255)
      Valid=0
   else if (Octets3 < 0 || Octets3 > 255)
      Valid=0
   else if (Octets4 < 0 || Octets4 > 255)
      Valid=0
   else Valid=1

   Oct1:=Octets1*0
   Oct2:=Octets2*0
   Oct3:=Octets3*0
   Oct4:=Octets4*0

   if (Oct1 <> 0 || Oct2 <> 0 || Oct3 <> 0 || Oct4 <> 0)
      Valid=0

   return %Valid%
}


Thanks for sharing,
OctalMage
Back to top
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Thu Jul 26, 2007 10:22 am    Post subject: Reply with quote

There is a bit of redundancy in the tests, and once you see it is not valid, there is no point to continue the tests. And Return don't need %% around the variable, although it is intelligent enough to behave correctly here.
And 0.0.0.0 is a valid IP address (default route or loopback, I don't recall exactly).
And of course, it addresses only one form of formatting: it can be a single 32-bit number, for example. But indeed, that's the most commonly used form, so it is OK.

So here is a slightly improved version of your code:
Code:
ValidIP(IPAddress)
{
   if (StrLen(IPAddress) > 15)
      Return 0

   IfInString, IPAddress, %A_Space%
      Return 0

   StringSplit, Octets, IPAddress, .
   if (Octets0 <> 4)
      Return 0
   Loop 4
   {
      If Octets%A_Index% is not digit
         Return 0
      If (Octets%A_Index% < 0 or Octets%A_Index% > 255)
         Return 0
   }

   return 1
}
ips =
(
0.0.0.0
127.0.0.1
10.0.0.40
192.168.1.12
255.255.255.255

5.5.5
256.4.3.1
7.-5.2.4
77.kk.99.1
1.2..4
11.22 .33.44
)
Loop Parse, ips, `n, `r
{
   r .= A_LoopField . " -> " . ValidIP(A_LoopField) . "`n"
}
MsgBox %r%

And using regular expressions can dramatically reduce the code size and speed up the process:
Code:
ValidIP(IPAddress)
{
   fp := RegExMatch(IPAddress, "^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$", octet)
   If (fp = 0)
      Return 0   ; Not matching: something is wrong
   Loop 4
   {
      If (octet%A_Index% > 255)
         Return 0
   }

   return 1
}

I could make a RE to do also the bound check, but it would be quite complex, might not worth the effort (except for the intellectual challenge).

Oh, and PentePirate/OctalMage, answering yourself with another nickname, praising your code is a bit... corny? Silly? Childish?

The irony is that is your IP address that blown away your cover (that, and the similar signatures... Very Happy)
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
Titan



Joined: 11 Aug 2004
Posts: 5048
Location: imaginationland

PostPosted: Thu Jul 26, 2007 10:44 am    Post subject: Reply with quote

I don't think you need a series of Ifs or intellectually challenging regex because this will do:

Code:
ValidIP(adr) {
   Loop, Parse, adr, .
      err += A_Index > 4 or A_LoopField < 0 or A_LoopField > 255
   Return, !err
}

_________________

RegExReplace("irc.freenode.net/ahk", "^(?=(.(?=[\0-r\[]*((?<=\.).))))(?:[c-\x73]{2,8}(\S))+((2)|\b[^\2-]){2}\D++$", "$u3$1$3$4$2")
Back to top
View user's profile Send private message Visit poster's website
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Thu Jul 26, 2007 2:02 pm    Post subject: Reply with quote

You should test it with my little testset and refine it a bit (3 false positives) but of course, you are right.
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
haichen



Joined: 05 Feb 2007
Posts: 108
Location: Osnabrück, Germany

PostPosted: Thu Jul 26, 2007 3:08 pm    Post subject: Reply with quote

I think this must be all. Right?

Code:
ValidIP(adr) {
   Loop, Parse, adr, .
   {
     err += A_LoopField < 0 or A_LoopField > 255
     loopcount := A_Index
   }
   err += loopcount <> 4
   IfInString, adr, %A_Space%
     err := true
   IfInString, adr, %A_Tab%
     err := True
   If adr = ""
     err := True
   Return, !err
}
Back to top
View user's profile Send private message
Titan



Joined: 11 Aug 2004
Posts: 5048
Location: imaginationland

PostPosted: Thu Jul 26, 2007 4:45 pm    Post subject: Reply with quote

PhiLho wrote:
You should test it with my little testset and refine it a bit (3 false positives) but of course, you are right.
Sorry I was not providing a full solution but rather a demonstration of a parsing loop. Since you asked however:

Code:
ValidIP(a) {
   Loop, Parse, a, .
      e += 1 + (A_LoopField < 0 or A_LoopField > 255 or (0 . A_LoopField . 0) + 0 = "")
   Return, e = 4
}

This version is shorter and probably quite faster since it uses AutoHotkey's built-in checking methods:
Code:
ValidIP(a) {
   Loop, Parse, a, .
      If A_LoopField is digit
         If A_LoopField between 0 and 255
            e++
   Return, e = 4
}

I am sure that there are similar API functions with IPv6 support. You can check that out too.
_________________

RegExReplace("irc.freenode.net/ahk", "^(?=(.(?=[\0-r\[]*((?<=\.).))))(?:[c-\x73]{2,8}(\S))+((2)|\b[^\2-]){2}\D++$", "$u3$1$3$4$2")
Back to top
View user's profile Send private message Visit poster's website
ManaUser



Joined: 24 May 2007
Posts: 901

PostPosted: Thu Jul 26, 2007 5:12 pm    Post subject: Reply with quote

You rock, however it does need one more check.
Code:
ValidIP(a) {
   Loop, Parse, a, .
   {
      If A_LoopField is digit
         If A_LoopField between 0 and 255
            e++
      c++
   }
   Return, e = 4 AND c = 4
}

Otherwise 123.42.64.banana.8 is valid.
Back to top
View user's profile Send private message
xXJa50nXx



Joined: 19 Oct 2006
Posts: 32
Location: mo

PostPosted: Thu Jul 26, 2007 6:08 pm    Post subject: Reply with quote

lol,

I do see the irony, but the truth is that Octal Mage=me, and PentePirate!=me.

We attend the same school.
Back to top
View user's profile Send private message Send e-mail
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Mon Jul 30, 2007 9:42 am    Post subject: Reply with quote

Ah, OK. That's the limitation of IP addressing, and alas IP banning: some unrelated people (same school, same work, same house...) share the same external IP address.
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group