Forum Discussion
Mauricio_Cusine
Aug 22, 2007Nimbostratus
Validate 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
- hooleylistCirrostratusWhat 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.
\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
\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
- hooleylistCirrostratusYour 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.
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" }
- Mauricio_CusineNimbostratusHello Aaron,
- hooleylistCirrostratusCan 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?
- Mauricio_CusineNimbostratusHelllo Aaron,
- qqdixf5_74186NimbostratusHi Aaron,
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" }
- hooleylistCirrostratusGood 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 $.
- 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" }
- The_BhattmanNimbostratusAlready added Natty's validation to http://devcentral.f5.com/wiki/default.aspx/iRules/IP__addr.html
- Thanks for doing that cmbhatt! Also, thanks for the getfield addition as well!
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