What could you do with your code in 20 Lines or Less? That's the question I ask every week, and every week I go looking to find cool new examples that show just how flexible and powerful iRules can be without getting in over your head.

This week we've got three more fun examples of iRules goodness for you, thanks to the awesome community driving the forums and the CodeShare.  As always the goal is to show off some of the things that you can do with iRules in less than 21 lines of code. Let's dig in:

Distribute Email by Source IP


From the wiki comes this handy showcase of pool switching based on IP address. Putting this to good use by distributing info to different mail pools is an even cooler idea, which is just  what this iRule does.

when CLIENT_ACCEPTED {   if { [IP::addr [IP::remote_addr] equals ] }  {     log local0. "Node IP address is: [IP::remote_addr] and sent to SMTP_clients_from_10.2"     pool smtp_clients_from_10.2   } else {     log local0. "Node IP address is: [IP::remote_addr] and sent to SMTP_clients_from_elsewhere"     pool SMTP_clients_from_elsewhere   }


String Based HTTPS Redirect


Yet another cool twist on the simple HTTP redirect iRules we've talked about before, this one uses a Location header in the response as well as a class of URIs to pick from. Even though it's a variation on a theme, it's cool to see the ways people are doing things, and remind us all that we have LOTS of options when it comes to iRuling.

 when HTTP_REQUEST {  
switch -glob [string tolower [HTTP::uri]] {
"*alumni/giving/gift/" -
"*alumni/giving/pledge/" -
"*alumni/directory/search.aspx" -
"*alumni/directory/update.aspx" {
# don't do anything...
default {
HTTP::respond 301 Location "http://[getfield [HTTP::host] : 1][HTTP::uri]"


HTTP Routing without HTTP Profile


This one is super cool (assuming it works..note that it's untested, but I like it anyway!). The issue here was that the user needed to inspect some HTTP information in a session without applying an HTTP profile to the Virtual Server. That means no HTTP:: commands. Oh no! Never fear, TCP::collect to the rescue!


set idx [string first " HTTP/1." [TCP::payload]]
if { $idx < 0 } {
if { [TCP::payload length] > 2048 } {
log local0. "ERROR! Could not find HTTP request in 2K! dropping..."
} else {
# Not enough data yet; collect more
set request [string tolower [TCP::payload $idx]]
log local0. "Got request: $request"
if { $request contains " /XXXXX" } {
log local0. "Sending to Pool_XXXXX"
pool Pool_XXXXX
} else {
log local0. "Sending to Pool_WWWWW"
pool Pool_WWWWW
Hot off the presses, that's this week's 20LoL. Tune in again next week for even more iRule endeavors.