Forum Discussion

Brian_Gibson_30's avatar
Brian_Gibson_30
Icon for Nimbostratus rankNimbostratus
Feb 19, 2015

Transforming an IP address via IRule

I am trying to take a client IP and convert it to a new IP using a generic IP address mask but using the last 2 octets of the client's IP.

My RegEx skills are not the greatest but this really doesn't seem that it should be that hard. So I'm not sure what I am doing wrong here as the regex seems ok. I tried to use braces on the regex but the LB said that my pattern didn't do anything then.

when CLIENT_ACCEPTED {

    set clientip [IP::client_addr]

    regsub "^\d+.\d+." $clientip "10.99." newip
    log local0.info "value on transformed IP is $newip" 
    }

4 Replies

  • better to use scan in this case:

    when CLIENT_ACCEPTED {
      scan [IP::client_addr] %d.%d.%d.%d 0 0 o3 o4
      log local0.info "value on transformed IP is 10.99.$o3.$o4"
    }
    
  • there's almost always a better way with string commands than using regex. Using regex is a very expensive operation for iRules, and should be avoided unless absolutely necessary.