# iRule with switch

**URL:** <https://community.f5.com/t/irule-with-switch/33604>\
**Category:** Technical Forums\
**Tags:** irules, application-delivery\
**Created:** [June 7, 2018, 3:49pm UTC](https://community.f5.com/t/irule-with-switch/33604 "2018-06-07T15:49:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeaV97\_339056](https://avatars.discourse-cdn.com/v4/letter/l/8797f3/32.png) [@LeaV97\_339056](https://community.f5.com/u/LeaV97_339056)\
**Post date:** [June 7, 2018, 3:49pm UTC](https://community.f5.com/t/irule-with-switch/33604/1 "2018-06-07T15:49:32Z")

</div>

I have an application that needs to be routed based on the incoming port . They have 5 different pools each with 25 ports. I’m thinking 1 VS with all ports open and then using an iRule to control the pool selection. I’ve found a lot of examples with 3-4 ports or a single port range but none with 125 ports in 5 different ranges. I’m looking at either a switch or a data group. Can you use a range in a switch? Can you use a range in a datagroup or does it have to be an exact match?

I’m sure my syntax is wrong but something like this:

when CLIENT\_ACCEPTED { switch [TCP::local\_port] { 10000-10024 { pool pool-site1 } 10025-10049 { pool pool-site2 } 10050-10074 { pool pool-site3 } default { pool default\_pool } }

---

<div class="post-metadata">

**Author:** ![leonline\_225556](https://avatars.discourse-cdn.com/v4/letter/l/5e9695/32.png) [@leonline\_225556](https://community.f5.com/u/leonline_225556)\
**Post date:** [June 7, 2018, 7:20pm UTC](https://community.f5.com/t/irule-with-switch/33604/2 "2018-06-07T19:20:59Z")

</div>

Hi LeaV97,

Using the regexp argument with the switch command you can easily match on numeric ranges. The following iRule will probably help you.

```tcl
when CLIENT_ACCEPTED
{

    switch -regexp [TCP::local_port] 
    { 
        [10000-10024] { pool pool-site1 } 
        [10025-10049] { pool pool-site2 } 
        [10050-10074] { pool pool-site3 } 
        default { pool default_pool } 
    }
}

```

[http://www.tcl.tk/man/tcl8.4/TclCmd/switch.htmM8](http://www.tcl.tk/man/tcl8.4/TclCmd/switch.htmM8)

---

<div class="post-metadata">

**Author:** ![LeaV97\_339056](https://avatars.discourse-cdn.com/v4/letter/l/8797f3/32.png) [@LeaV97\_339056](https://community.f5.com/u/LeaV97_339056)\
**Post date:** [June 7, 2018, 8:20pm UTC](https://community.f5.com/t/irule-with-switch/33604/3 "2018-06-07T20:20:43Z")

</div>

Thank you!

Can you do the same with a datagroup or is that only a 1:1 mapping?

---

<div class="post-metadata">

**Author:** ![JG](https://avatars.discourse-cdn.com/v4/letter/j/dfb087/32.png) [@JG](https://community.f5.com/u/JG)\
**Post date:** [June 7, 2018, 11:25pm UTC](https://community.f5.com/t/irule-with-switch/33604/4 "2018-06-07T23:25:11Z")

</div>

Just curious, but why do you prefer a possible data group solution?

---

<div class="post-metadata">

**Author:** ![stan\_piron](https://avatars.discourse-cdn.com/v4/letter/s/7bcc69/32.png) [@stan\_piron](https://community.f5.com/u/stan_piron)\
**Post date:** [June 8, 2018, 7:40am UTC](https://community.f5.com/t/irule-with-switch/33604/5 "2018-06-08T07:40:36Z")

</div>

you can also use this:

```tcl
set port [TCP::local_port]
switch 1 \
[expr {$port>=10000 && $port<10025}] {pool pool-site1} \
[expr {$port>=10025 && $port<10050}] {pool pool-site2} \
[expr {$port>=10050 && $port<10075}] {pool pool-site3} \
default {pool default_pool}

```

Make sure you let 1 in switch value, each expression is evaluated and the result is compared with **1** until one success.

This switch command can’t have curly brackets because of expression evaluation, that’s why the \ is mandatory at the end of each line but the last.

---

<div class="post-metadata">

**Author:** ![LeaV97\_339056](https://avatars.discourse-cdn.com/v4/letter/l/8797f3/32.png) [@LeaV97\_339056](https://community.f5.com/u/LeaV97_339056)\
**Post date:** [June 8, 2018, 11:57am UTC](https://community.f5.com/t/irule-with-switch/33604/6 "2018-06-08T11:57:18Z")

</div>

Supportability, operations knows how to edit a data group. They don’t have access to edit an iRule.
