Forum Discussion

Ajit's avatar
Ajit
Icon for Altostratus rankAltostratus
Oct 19, 2014

Add 2 source ip addresses to switch in irule

I need to add two more src ip addresses 1.1.1.1 & 2.2.2.2 along with 66.78.30.7 so that if traffic comes from any of these src ip addresses it should be redirected to the same pool. Can anyone help me with the syntax of the same. Thanks in advance

 

ltm rule ggi-pool-select { when HTTP_REQUEST { set srcip [IP::client_addr] set host [string tolower [HTTP::host]] switch $srcip { 66.78.30.7 { switch $host { "xyz.abc.net" { pool xyz.abc.net-ggi-https } } } } } }

 

1 Reply

  • If you want to keep the iRule as it is, just add the new IP addresses as below:

    when HTTP_REQUEST {
    
      set srcip [IP::client_addr]
      set host [string tolower [HTTP::host]]
    
      switch $srcip { 
      "1.1.1.1" - 
      "2.2.2.2" -
      "66.78.30.7" {
        switch $host {
          "xyz.abc.net" {
            pool xyz.abc.net-ggi-https
          }
        }
      } 
    }
    }
    

    However, your current solution needs some improvements. I'd recommend something as below that does the exact same job:

    when HTTP_REQUEST {
    
      if { [string tolower [HTTP::host]] == "xyz.abc.net" } {
        switch [IP::client_addr] { 
          "1.1.1.1" - 
          "2.2.2.2" -
          "66.78.30.7" {
            pool xyz.abc.net-ggi-https
          }
        }
      } 
    }
    

    Recommendations: 1 - Don't define variables, unless you really have to (waste of memory) 2 - Use of the IF-statement is preferred in case of a single condition. SWITCH-statement is better when you have 3 or more conditions.