Forum Discussion
Mauricio_Cusine
Nimbostratus
Aug 22, 2007Validate IP address with regexp
Hello,
I am trying to check if a IP address is valid in the bigip with regexp (I don't know if anybody know other method).
The regexp is the follow but don't work. Anybody can help me?.
I need to check if the ip address is in the range 1-255 in the 4 octects.
King regards,
Mauro
regexp
\b(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]?)\b
13 Replies
- hoolio
Cirrostratus
What is happening when you test your regex? Does it just not match, or do you get a TCL error in the LTM log file (/var/log/ltm). It is hard to see what you've used, as DC munges the characters.
Here are two options from regexbuddy:
I've attached them in a file in case DC munges the characters.
Do you want to capture the match in backreferences? The first option doesn't use backreferences, the second one does:
IP address
Matches 0.0.0.0 through 255.255.255.255
Use this regex to match IP numbers with accuracy, without access to the individual IP numbers.\b(??:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b
IP address
Matches 0.0.0.0 through 255.255.255.255
Use this regex to match IP numbers with accuracy.
Each of the 4 numbers is stored into a capturing group, so you can access them for further processing.\b(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]?)\b
Aaron - hoolio
Cirrostratus
Your original version works for me. Here is a quick method to test. If you change ::ip_address to an invalid IP, no match is found. If it is valid, a match is logged.
If the display of this regex is broken, you should be able to see it correctly by clicking reply and looking at the post.when RULE_INIT { set ::match {} set ::ip_address "256.2.3.4" set ::ip_address_regex {\b(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]?)\b} regexp $::ip_address_regex $::ip_address ::match log local0. "\$::ip_address: $::ip_address; matched: $::match" }
Aaron - Mauricio_Cusine
Nimbostratus
Hello Aaron,
Thanks very much for your answer.
When We applyed the Irule matched ip address out of range like as 400.400.400.400 and good ip address like as 100.100.100.100, but We don't receive any TCL error in /var/log/ltm.
So after saw you last comment We think that We are using bad the if with the regexp, what do you think this is the coded used.
One important note is that in the 4 octect Trash is added after the number for some motive, I suspect that for this reason don't work fine the regexp??.
Also We want to the regexp detect not only ip address out of range and also string, or other special caracter as a invalid ip address, We think the regexp does it.
King regards,
Mauricio
This is a code extract using the regexp
when CLIENT_DATA {
Capturamos la IP del Payload
set vacio " "
set IP ""
set LoginIP ""
set LoginIP "[findstr [TCP::payload] LOGIN-IP 10 MSISDN]"
log local0. "LoginIP= $LoginIP"
se verifica que el valor de LOGIN-IP es vacio
if {$LoginIP= ""}{
log local0. "Pool por defecto"
pool LDAP_IRULE_10}
else {
if { [regexp "(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]?)" $LoginIP]}{
log local0."evaluada regular expresion = $LoginIP" - hoolio
Cirrostratus
Can you replace the double quotes with curly braces {}? Can you post with some examples of the $LoginIP values and the matches that you're seeing?
Thanks,
Aaron - Mauricio_Cusine
Nimbostratus
Helllo Aaron,
I will try with {} so I see for example if I test with : 10.10.10.100 I receive in the F5 10.10.10.1000 or 100L in the last octect. So I capture the Ip from the TCP playload and is not a IP filed the IP came from string parameter.
This is I need to validate :
ldapsearch -h ip_virtual f5 -p 389 -b o=SIUX "LOGIN-IP=X.X.X.X" MSISDN the x.x.x.x is that I need to check if a valid ip address and is not a caracter.
King regards,
Mauricio - qqdixf5_74186
Nimbostratus
Hi Aaron,
I was trying out your test rule for ip address matching. With a vliad ip address as follow, no match was found. Please advice. Thank you!when RULE_INIT { set ::match {} set ::ip_address "10.10.10.10" set ::ip_address_regex {\b(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]?)\b} regexp $::ip_address_regex $::ip_address ::match log local0. "\$::ip_address: $::ip_address; matched: $::match" } - hoolio
Cirrostratus
Good to hear. By the way, mauricio01, I think the issue you were having was because you were saving the variable with the $ in the variable name. When setting variables in TCL, you leave off the leading $.
Aaron - Regexps are not always the best solution. If all you are concerned about is whether the string is in the form of a.b.c.d where a,b,c,d are between 1 and 255 inclusive, how about this solution:
set addr "10.10.10.10" set a [getfield $addr "." 1] set b [getfield $addr "." 2] set c [getfield $addr "." 3] set d [getfield $addr "." 4] if { (1 <= $a) && ($a <= 255) && (1 <= $b) && ($b <= 255) && (1 <= $c) && ($c <= 255) && (1 <= $d) && ($d <= 255) } { log local0. "$addr is a valid IP Address" } else { log local0. "$addr is NOT a valid IP Address" }
This will definitely be less CPU intensive than running the regular expression and should cover all the corner cases.
-Joe - The_Bhattman
Nimbostratus
Already added Natty's validation to http://devcentral.f5.com/wiki/default.aspx/iRules/IP__addr.html
Thanks Natty. - Thanks for doing that cmbhatt! Also, thanks for the getfield addition as well!
-Joe
Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects