Forum Discussion
Load balance based on the source IP addresses 4th octet
Hello, I’m attempting to create an iRule that will load balance based on the source IP addresses 4th octet and based on the availability of pool members. If it’s odd, I want it to redirect to http://test1.com. If it’s even, I want it to redirect to http://test2.com.
Below is the irule I created:
when CLIENT_ACCEPTED { set last [lindex [split [IP::client_addr] "."] 3] log local0. "CLIENT IP address [IP::client_addr]" log local0. "4th octect $last" if { ($last and end_with (3,5,7,9)) } { set destination odd } else { set destination even } }
when HTTP_REQUEST { if { ( [$destination eq odd] ) and
( [active_members test01_pool] > 0 )} { HTTP::redirect http://test1.com[HTTP::uri] } elseif { ( [$destination eq odd] ) and
( [active_members test02_pool] > 0 )} { HTTP::redirect http://test2.com[HTTP::uri] } elseif { ( [$destination eq even] ) and
( [active_members test02_pool] > 0 ) } { HTTP::redirect http://test2.com[HTTP::uri] } elseif { ( [$destination eq even] ) and
( [active_members test01_pool] > 0)} { HTTP::redirect http://test1.com[HTTP::uri] } }
I keep getting the following message when I test:
Oct 31 17:43:29 bigip info tmm[13447]: Rule /Common/Med-v2 : CLIENT IP address 10.128.10.1 Oct 31 17:43:29 bigip info tmm[13447]: Rule /Common/Med-v2 : 4th octect 1 Oct 31 17:43:29 bigip err tmm[13447]: 01220001:3: TCL error: /Common/Med-v2 - invalid command name "::1" while executing "::$last end_with (3,5,7,9)"
4 Replies
- R_Marc
Nimbostratus
there are more odds than 3,5, 7 and 9.
To detect if something is even or odd you should use modulus.
pseudo code: if { $X%2 } { odd } else { even }
For example:
cat test.tcl if { 1025%2 } { puts "odd" } else { puts "even" } if { 666%2 } { puts "odd" } else { puts "even" } tclsh test.tcl odd even - Kmussa_164917
Nimbostratus
Thank you.
- Kmussa_164917
Nimbostratus
Thanks to R Mac, I’m able to learn if the fourth octet is odd or even. However, I’m still not able to redirect based on this information.
when CLIENT_ACCEPTED { set fourthoctet [lindex [split [IP::client_addr] "."] 3] log local0. "CLIENT IP address [IP::client_addr]" log local0. "4th octect $fourthoctet" if { $fourthoctet%2} { log local0. "[IP::client_addr] is odd" } else { log local0. "[IP::client_addr] is even" } }
when HTTP_REQUEST { if { ( { $fourthoctet%2} ) and
( [active_members test01_pool] > 0 )} { HTTP::redirect http://test01.com[HTTP::uri] } elseif { ( { $fourthoctet%2} ) and
( [active_members test02_pool] > 0 )} { HTTP::redirect http://test02.com[HTTP::uri] } elseif { ( { $fourthoctet%2} ) and
( [active_members test02_pool] > 0 ) } { HTTP::redirect http://test02.com[HTTP::uri] } elseif { ( { $fourthoctet%2} ) and
( [active_members test01_pool] > 0)} { HTTP::redirect http://test01.com[HTTP::uri] } } - R_Marc
Nimbostratus
I believe you need to change ( { $fourthoctet%2} ) to just ( $fourthoctet%2 ).
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
* 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
