# Source IP Based Pool Routing

**URL:** <https://community.f5.com/t/source-ip-based-pool-routing/64881>\
**Category:** Community CodeShare\
**Tags:** irules, devops, application-delivery, pool\
**Created:** [January 30, 2015, 12:51pm UTC](https://community.f5.com/t/source-ip-based-pool-routing/64881 "2015-01-30T12:51:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![CodeCentral\_194](https://avatars.discourse-cdn.com/v4/letter/c/c77e96/32.png) [@CodeCentral\_194](https://community.f5.com/u/CodeCentral_194)\
**Post date:** [January 30, 2015, 12:51pm UTC](https://community.f5.com/t/source-ip-based-pool-routing/64881/1 "2015-01-30T12:51:38Z")

</div>

**Problem this snippet solves:**

I had an issue where my company wanted to have more than one mail server sending mail out of our building, they wanted to start sending ¨information emails¨ out but we found that we started to get black listed by mail servers on our main email server due to the ¨information¨ mail servers were using it’s external address to send. To get around this I have written this iRule (with the help of the Dev Central Guys) which looks at the source IP of the client and then re-directs to a set pool, then if the source address doesn’t match it will use a default pool. You can use this iRule to point any port as long as a virtual server is there listening for that port, you will also need a SNAT pool too.

**Code :**

```auto
when CLIENT_ACCEPTED {

if { [IP::client_addr] equals "*.*.*.*" } {
   pool ¨enter pool name¨
   
#Uncomment the line below to turn on logging.
   #log local0. "Valid client IP: [IP::client_addr] - HostName"

} elseif { 

[IP::client_addr] equals "*.*.*.*" } {
   pool ¨enter pool name¨

   #Uncomment the line below to turn on logging. 
   #log local0. "Valid client IP: [IP::client_addr] - HostName"

} elseif { 

[IP::client_addr] equals "*.*.*.*" } {
   pool ¨enter pool name¨

   #Uncomment the line below to turn on logging. 
   #log local0. "Valid client IP: [IP::client_addr] - HostName"

} elseif {

[IP::client_addr] equals "*.*.*.*" } {
   pool ¨enter pool name¨

   #Uncomment the line below to turn on logging. 
   #log local0. "Valid client IP: [IP::client_addr] - HostName"

} else {

snatpool ¨enter pool name¨

#Uncomment the line below to turn on logging.
                #log local0. "Valid client IP: [IP::client_addr] - HostName"

} 
}

### update by cmbhatt to switch ###

when CLIENT_ACCEPTED {
switch -glob [IP::client_addr] {
  "*.*.*.*" { 
                 #Uncomment the line below to turn on logging.         
                 #log local0. "Valid client IP: [IP::client_addr] - HostName"
                 pool poolname 
                    }
  "*.*.*.*" { 
                 #Uncomment the line below to turn on logging.         
                 #log local0. "Valid client IP: [IP::client_addr] - HostName"
                 pool poolname
                     }
  default {
                 #Uncomment the line below to turn on logging.         
                 #log local0. "Valid client IP: [IP::client_addr] - HostName"
                 snatpool pooname3 
                  }
}
}

```
