For more information regarding the security incident at F5, the actions we are taking to address it, and our ongoing efforts to protect our customers, click here.

Forum Discussion

f5_rocks's avatar
f5_rocks
Icon for Nimbostratus rankNimbostratus
May 06, 2016

I'm trying to write an irule with multiple not scripts in it

I'm trying to write an irule with multiple not scripts in it

 

13 Replies

  • I'm working on a project which requires migrating most of the urls for an application suite to similar, but different hostnames. I started with this irule for one url string, but now, they want to keep some of the urls the way they were and some not, so the irule comparison needs to start at a higher level, but not redirect some specific substrings.

     

    I started with this:

     

    when CLIENT_ACCEPTED {event HTTP_REQUEST enable} when HTTP_REQUEST { set MY_HOST [string tolower [HTTP::host]] set MY_URI [string tolower [HTTP::uri]]

     

    if {$MY_URI starts_with "/abcd/web/abcd.client.web/"} { if {$MY_HOST equals "m3.companyname.com"} { HTTP::redirect http://a1.m3.companyname.com$MY_URI } elseif {$MY_HOST equals "internal.m3.companyname.com"} { HTTP::redirect http://internal.a1.m3.companyname.com$MY_URI } elseif {$MY_HOST equals "internal.m3-dc1.companyname.com"} { HTTP::redirect http://internal.a1.m3-dc1.companyname.com$MY_URI } elseif {$MY_HOST equals "internal.m3-dc2.companyname.com"} { HTTP::redirect http://internal.a1.m3-dc2.companyname.com$MY_URI } elseif {$MY_HOST equals "internal.m3-dc3.companyname.com"} { HTTP::redirect http://internal.a1.m3-dc3.companyname.com$MY_URI } event HTTP_REQUEST disable TCP::close } }

     

    when HTTP_RESPONSE {event HTTP_REQUEST enable}

    tried this, get syntax/format errors

     

    when CLIENT_ACCEPTED {event HTTP_REQUEST enable} when HTTP_REQUEST { set MY_HOST [string tolower [HTTP::host]] set MY_URI [string tolower [HTTP::uri]]

     

    if {$MY_URI starts_with "/abcd/"} { if {not ($MY_URI starts_with "/abcd/web/abcdpayment.web/") } elseif {not ($MY_URI starts_with "/abcd/web/abcdpaymentprocessing.web/")} { if {$MY_HOST equals "m3.companyname.com"} { HTTP::redirect http://a1.m3.companyname.com$MY_URI } elseif {$MY_HOST equals "internal.m3.companyname.com"} { HTTP::redirect http://internal.a1.m3.companyname.com$MY_URI } elseif {$MY_HOST equals "internal.m3-dc1.companyname.com"} { HTTP::redirect http://internal.a1.m3-dc1.companyname.com$MY_URI } elseif {$MY_HOST equals "internal.m3-dc2.companyname.com"} { HTTP::redirect http://internal.a1.m3-dc2.companyname.com$MY_URI } elseif {$MY_HOST equals "internal.m3-dc3.companyname.com"} { HTTP::redirect http://internal.a1.m3-dc3.companyname.com$MY_URI } event HTTP_REQUEST disable TCP::close } } }

     

    when HTTP_RESPONSE {event HTTP_REQUEST enable}

    could use "or", f5 accepted it as syntactically correct, but this try didn't work, still redirected the "not" uris...

     

    when CLIENT_ACCEPTED {event HTTP_REQUEST enable} when HTTP_REQUEST { set MY_HOST [string tolower [HTTP::host]] set MY_URI [string tolower [HTTP::uri]]

     

    if {$MY_URI starts_with "/abcd/"} { if {not ($MY_URI starts_with "/abcd/web/abcdpayment.web/") or not ($MY_URI starts_with "/abcd/web/abcdpaymentprocessing.web/") } { if {$MY_HOST equals "m3.companyname"} { HTTP::redirect $MY_URI } elseif {$MY_HOST equals "internal.m3.companyname"} { HTTP::redirect $MY_URI } elseif {$MY_HOST equals "internal.m3-dc1.companyname"} { HTTP::redirect $MY_URI } elseif {$MY_HOST equals "internal.m3-dc2.companyname"} { HTTP::redirect $MY_URI } elseif {$MY_HOST equals "internal.m3-dc3.companyname"} { HTTP::redirect $MY_URI } event HTTP_REQUEST disable TCP::close } } } when HTTP_RESPONSE {event HTTP_REQUEST enable}

     

    I thought of using a generic "contains", of "/abcd/web/abcdpayment" but I expect more very specific surgical type requests may be desired in the future.

     

    I think using "or" or some other type of comparison must be the better choice, but I may not be skilled enough to find the answer in the time window required.

     

    I appreciate any help you could offer. (I don't know why the last line in the irule is blown up so large)

     

    • f5_rocks's avatar
      f5_rocks
      Icon for Nimbostratus rankNimbostratus
      Sorry about the formatting. It's my first question and I'm trying to figure out how to keep the original formatting. I'll look at switch....Well, I figured out how to format properly, but the forum's algorithims think it's spam, so...
    • Yann_Desmarest's avatar
      Yann_Desmarest
      Icon for Cirrus rankCirrus
      for the formatting, you can post a new answer, select the whole code and click on the Preformatted Code button in the pseudo wysiwyg toolbar
    • f5_rocks's avatar
      f5_rocks
      Icon for Nimbostratus rankNimbostratus
      Yes. Thank you. I reposted as another question using proper formatting and got an answer on how to write the script using "if {not (a or b)}" or "if {not (a) and not (b)}. I was misunderstanding the correct usage and result of "or".
    • f5_rocks's avatar
      f5_rocks
      Icon for Nimbostratus rankNimbostratus
      Sorry about the formatting. It's my first question and I'm trying to figure out how to keep the original formatting. I'll look at switch....Well, I figured out how to format properly, but the forum's algorithims think it's spam, so...
    • Yann_Desmarest_'s avatar
      Yann_Desmarest_
      Icon for Nacreous rankNacreous
      for the formatting, you can post a new answer, select the whole code and click on the Preformatted Code button in the pseudo wysiwyg toolbar
    • f5_rocks's avatar
      f5_rocks
      Icon for Nimbostratus rankNimbostratus
      Yes. Thank you. I reposted as another question using proper formatting and got an answer on how to write the script using "if {not (a or b)}" or "if {not (a) and not (b)}. I was misunderstanding the correct usage and result of "or".
  • Hi,

    you can try to use array to define old and new hostnames:

    when RULE_INIT {
        array set static::NewURL {
            m3.companyname              a1.m3.companyname
            internal.m3.companyname     internal.a1.m3.companyname
            internal.m3-dc1.companyname internal.a1.m3-dc1.companyname
            internal.m3-dc2.companyname internal.a1.m3-dc2.companyname
            internal.m3-dc3.companyname internal.a1.m3-dc3.companyname
        }
    }
    
    
    when HTTP_REQUEST {
        switch -glob [string tolower [HTTP::path]] {
            "/abcd/web/abcdpayment.web/*" -
            "/abcd/web/abcdpaymentprocessing.web/*" {}
            "/abcd/*" {
                if {[info exists static::NewURL([HTTP::host])]} {HTTP::redirect http://$static::NewURL([HTTP::host])[string tolower [HTTP::uri]]}
            }
        }
    }