# SNAT iRule

**URL:** <https://community.f5.com/t/snat-irule/363>\
**Category:** Technical Forums\
**Tags:** irules, dev, devops, application-delivery\
**Created:** [February 15, 2013, 7:00pm UTC](https://community.f5.com/t/snat-irule/363 "2013-02-15T19:00:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![thebishop\_10153](https://avatars.discourse-cdn.com/v4/letter/t/9fc29f/32.png) [@thebishop\_10153](https://community.f5.com/u/thebishop_10153)\
**Post date:** [February 15, 2013, 7:00pm UTC](https://community.f5.com/t/snat-irule/363/1 "2013-02-15T19:00:11Z")

</div>

I am trying to write a SNAT rule that will use a data group to map the client address to a specific SNAT in version 10.2.3. Due to the protocol, we need this functionality to allow traceability in our environment. The iRule never returns a value from the data group. I have tried many formats for the string group, but no luck in matching. Am I not formating the data group correctly or am I having trouble comparing the string to the address? I’ve tried many different ways to format the class match line as well.

class snat\_map {

{

“\“10.100.61.150\” := { \“10.146.1.7\” }”

“\“10.100.61.150\” { \“10.146.1.3\” }”

“10.100.61.150 := { \“10.146.1.4\” }”

“10.100.61.150 { \“10.146.1.6\”}”

“10.100.61.150 { 10.146.1.5 }”

“10.100.61.150:=10.146.1.3”

}

}

rule snat\_rule {

when CLIENT\_ACCEPTED {

TCP::collect 8

set srcip [IP::client\_addr]

log local0. “Entering iRule snat\_rule $srcip”

set snat\_address [class match -value “$srcip” equals snat\_map]

log local0. “snat\_address is $snat\_address”

if { “$snat\_address” ne “” } {

log local0. “Using address $snat\_address”

snat $snat\_address

}

Use default SNAT pool if not found

}

}

Feb 15 07:02:55 local/tmm1 info tmm1[2858]: Rule slp\_snap\_rule : Entering iRule slp\_snat\_rule 10.100.61.150

Feb 15 07:02:55 local/tmm1 info tmm1[2858]: Rule slp\_snap\_rule : snat\_address is

---

<div class="post-metadata">

**Author:** ![nitass](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/nitass/32/2270_2.png) [@nitass](https://community.f5.com/u/nitass)\
**Post date:** [February 16, 2013, 12:51am UTC](https://community.f5.com/t/snat-irule/363/2 "2013-02-16T00:51:22Z")

</div>

e.g.

```tcl
[root@ve10:Active] config b class snat_map list
class snat_map {
   "10.100.61.150" { "10.146.1.7" }
}
[root@ve10:Active] config b rule myrule list
rule myrule {
   when RULE_INIT {
   set srcip "10.100.61.150"
   log local0. "\[class match -value $srcip equals snat_map\] [class match -value $srcip equals snat_map]"
}
}

[root@ve10:Active] config cat /var/log/ltm
Feb 16 09:39:50 local/tmm info tmm[4909]: Rule myrule : [class match -value 10.100.61.150 equals snat_map] 10.146.1.7

```

---

<div class="post-metadata">

**Author:** ![thebishop\_10153](https://avatars.discourse-cdn.com/v4/letter/t/9fc29f/32.png) [@thebishop\_10153](https://community.f5.com/u/thebishop_10153)\
**Post date:** [February 18, 2013, 2:29pm UTC](https://community.f5.com/t/snat-irule/363/3 "2013-02-18T14:29:47Z")

</div>

I receive the following error when I use this format.

01070151:3: Rule [snat\_rule] error: line 6: [wrong args] [class match - value $srcip equals snat\_map]

If I put quotes around the srcip variable it will compile, but it won’t match anything in the class.

---

<div class="post-metadata">

**Author:** ![What\_Lies\_Bene1](https://avatars.discourse-cdn.com/v4/letter/w/59ef9b/32.png) [@What\_Lies\_Bene1](https://community.f5.com/u/What_Lies_Bene1)\
**Post date:** [February 18, 2013, 2:52pm UTC](https://community.f5.com/t/snat-irule/363/4 "2013-02-18T14:52:45Z")

</div>

Is the log command all on one line? It should be.

---

<div class="post-metadata">

**Author:** ![thebishop\_10153](https://avatars.discourse-cdn.com/v4/letter/t/9fc29f/32.png) [@thebishop\_10153](https://community.f5.com/u/thebishop_10153)\
**Post date:** [February 18, 2013, 2:54pm UTC](https://community.f5.com/t/snat-irule/363/5 "2013-02-18T14:54:09Z")

</div>

Yes, it is one line.

---

<div class="post-metadata">

**Author:** ![What\_Lies\_Bene1](https://avatars.discourse-cdn.com/v4/letter/w/59ef9b/32.png) [@What\_Lies\_Bene1](https://community.f5.com/u/What_Lies_Bene1)\
**Post date:** [February 18, 2013, 3:14pm UTC](https://community.f5.com/t/snat-irule/363/6 "2013-02-18T15:14:39Z")

</div>

OK, can you post your exact code?

---

<div class="post-metadata">

**Author:** ![thebishop\_10153](https://avatars.discourse-cdn.com/v4/letter/t/9fc29f/32.png) [@thebishop\_10153](https://community.f5.com/u/thebishop_10153)\
**Post date:** [February 18, 2013, 3:33pm UTC](https://community.f5.com/t/snat-irule/363/7 "2013-02-18T15:33:16Z")

</div>

Below if the latest.

when CLIENT\_ACCEPTED {

TCP::collect 8

set snat\_address [class match -value “[IP::client\_addr]” equals snat\_map]

log local0. “snat\_address is $snat\_address”

if { “$snat\_address” ne “” } {

snat $snat\_address

}

}

class snat\_map {

“\“10.100.61.150\” { \“10.146.1.7\” }”

---

<div class="post-metadata">

**Author:** ![What\_Lies\_Bene1](https://avatars.discourse-cdn.com/v4/letter/w/59ef9b/32.png) [@What\_Lies\_Bene1](https://community.f5.com/u/What_Lies_Bene1)\
**Post date:** [February 18, 2013, 4:17pm UTC](https://community.f5.com/t/snat-irule/363/8 "2013-02-18T16:17:22Z")

</div>

The rule code looks good to me. It saves OK on my v11.3 VE.

Did you miss the closing } for the Class configuration?

Interesting that the error references line 6, has the rule changed since you posted the error message?

---

<div class="post-metadata">

**Author:** ![Luis\_Araujo\_560](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@Luis\_Araujo\_560](https://community.f5.com/u/Luis_Araujo_560)\
**Post date:** [March 27, 2013, 8:14pm UTC](https://community.f5.com/t/snat-irule/363/9 "2013-03-27T20:14:07Z")

</div>

If I use the string in data group, the snat is executed in v11.x???

Tks

Luis
