Forum Discussion
Christopher_Da1
Nimbostratus
Aug 08, 2006iRule to manipulate designation content based off ip/subnet
First of all I’m new to iRules and not familiar with the syntax.
I have composed an irule and the editor says the syntax is correct, but it doesn’t behave correctly or the way I intend for it to.
In need to permit the following traffic to a specific pool.
156.74.138.63
156.74.12.223
156.74.12.222
156.74.16.0/24
If the source isn’t one of the above the traffic should be redirected to
http://inweb.ci.seattle.wa.us/ccss/noaccess.asp
When I access the vip the irule is applied to from 156.74.138.63 I get a “The page cannot be displayed” error. Statically I can see hits on the irule, however it doesn’t work and yes the web server in the pool is up.
HELP!!!
------------------------------------------------------------------------------
when CLIENT_ACCEPTED {
if { [IP::addr [IP::client_addr] equals 156.74.138.63] } {
pool GWWEB-HTTP
}
elseif { [IP::addr [IP::client_addr] equals 156.74.12.223] } {
pool GWWEB-HTTP
}
elseif { [IP::addr [IP::client_addr] equals 156.74.12.222] } {
pool GWWEB-HTTP
}
elseif {not[IP::addr [IP::client_addr] equals 156.74.16.0/24] } {
set issue_redirect 1 }
else
{ pool GWWEB-HTTP
}
}
when HTTP_REQUEST {
if { $issue_redirect == 1 } { redirect to "http://inweb.ci.seattle.wa.us/ccss/noaccess.asp" }
}
--------------------------------------------------------------------------------------
- Have you tried logging any data to see what's wrong with your logic? That should always be the first step in diagnosing a problem. One thing I noticed is that you'll want to use the HTTP::redirect command instead of "redirect to".
when CLIENT_ACCEPTED { log local0. "client address: [IP::client_addr]" if { [IP::addr [IP::client_addr] equals 156.74.138.63] } { log local0. "matched address 156.74.138.63" pool GWWEB-HTTP } elseif { [IP::addr [IP::client_addr] equals 156.74.12.223] } { log local0. "matched address 156.74.12.223" pool GWWEB-HTTP } elseif { [IP::addr [IP::client_addr] equals 156.74.12.222] } { log local0. "matched address 156.74.12.222" pool GWWEB-HTTP } elseif {not[IP::addr [IP::client_addr] equals 156.74.16.0/24] } { log local0. "matched subnet 156.74.16.0/24" set issue_redirect 1 } else { log local0. "didn't match any addresses" pool GWWEB-HTTP } } when HTTP_REQUEST { log local0. "issue_redirect: $issue_redirect" if { $issue_redirect == 1 } { log local0. "issuing redirect..." HTTP::redirect "http://inweb.ci.seattle.wa.us/ccss/noaccess.asp" } }
- Christopher_Da1
Nimbostratus
Thanks, - Looks like you'll have to specifically set that variable to zero to avoid the no such variable error.
when CLIENT_ACCEPTED { set issue_redirect 0 log local0. "client address: [IP::client_addr]" if { [IP::addr [IP::client_addr] equals 156.74.138.63] } { log local0. "matched address 156.74.138.63" pool GWWEB-HTTP } elseif { [IP::addr [IP::client_addr] equals 156.74.12.223] } { log local0. "matched address 156.74.12.223" pool GWWEB-HTTP } elseif { [IP::addr [IP::client_addr] equals 156.74.12.222] } { log local0. "matched address 156.74.12.222" pool GWWEB-HTTP } elseif {not[IP::addr [IP::client_addr] equals 156.74.16.0/24] } { log local0. "matched subnet 156.74.16.0/24" set issue_redirect 1 } else { log local0. "didn't match any addresses" pool GWWEB-HTTP } } when HTTP_REQUEST { log local0. "issue_redirect: $issue_redirect" if { $issue_redirect == 1 } { log local0. "issuing redirect..." HTTP::redirect "http://inweb.ci.seattle.wa.us/ccss/noaccess.asp" } }
- Christopher_Da1
Nimbostratus
That did it! - JRahm
Admin
If your IP matching is going to grow, you could build a class to clean up the rule. Also, since I assume that this is all HTTP traffic, you could move all the logic to the HTTP_REQUEST event to avoid setting the variable (or use info exists in the CLIENT_ACCEPTED event).class my_exceptions { "156.74.138.63 GWWEB-HTTP" "156.74.12.223 GWWEB-HTTP" "156.74.12.222 GWWEB-HTTP" "10.10.10.10 DIFFERENT_POOL-HTTP" } when HTTP_REQUEST { if { [matchclass [IP::client_addr] equals $::my_exceptions ] } { log local0. "matched address [IP::client_addr] " pool [findclass [IP::client_addr] $::my_exceptions " " ] } elseif { not ( [IP::addr [IP::client_addr] equals 156.74.12.223 ] ) } { log local0. "matched subnet 156.74.16.0/24, issuing redirect..." HTTP::redirect "http://inweb.ci.seattle.wa.us/ccss/noaccess.asp" } else { log local0. "didn't match any addresses" pool GWWEB-HTTP } }
- JRahm
Admin
I forgot to mention that I threw that 10.10.10.10 DIFFERENT_POOL-HTTP entry into the class to show you that you can specify different sources within the class with different destinations and the pool substiution will take care of that in one command. What the findclass statement is doing is looking up your client IP in the class and taking the value after the space, which happens to be your pool name. - Christopher_Da1
Nimbostratus
Thanks for the info. This irule is being put in place to keep unwanted traffic out during a test phase. If it becomes a permanent addition I will definitely change to your recommend logic. - Deb_Allen_18Historic F5 AccountIn iRules where the pool is dynamically selected, the "default" pool for traffic that falls through the rest of the conditions will be the last pool selected by the rule, rather than the default pool that is defined on the virtual server.
- JRahm
Admin
Great point. Whereas I think in this rule it would work without the else condition, setting it provides the necessary protection.
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