Forum Discussion

netadmindetail_'s avatar
netadmindetail_
Icon for Nimbostratus rankNimbostratus
Apr 22, 2015

Better way to Cleaning UP string variable

Hi all

 

I must use the destination FQND of a TCP connection. I'm able to collect the TCP payload but I need to compare it with a data-class. But before doing that I need to clean the TCP payload variable

 

Here's what I have to clean: ......9net.tcp://sub.domain.com:12345/abcdefg... I need this finish with this : sub.domain.com

 

What is the most efficient way to achieve this : using regexp, string trim,

 

I want in fact the less CPU intensive way.

 

Thanks

 

  • How about using scan?

    If the variable string is set '9net.tcp://sub.domain.com:12345/abcdefg' or 'sometotherstuff9net.tcp://sub.domain.com:12345/abcdefg/lkjsdflkj/sdfl' etc then try using

    scan $string {%*[^/]//%[^:]} fqdn

    this results in a new variable $fqdn of 'sub.domain.com'

    You can test it out by using the tclsh

    % set string "lkasdflkjwe234239net.tcp://sub.domain.com:12345/abcdefg/sdlfkjasf"
    lkasdflkjwe234239net.tcp://sub.domain.com:12345/abcdefg/sdlfkjasf
    % puts $string
    lkasdflkjwe234239net.tcp://sub.domain.com:12345/abcdefg/sdlfkjasf
    
    % scan $string {%*[^/]//%[^:]} fqdn
    1
    % puts $fqdn
    sub.domain.com
    

    Check out this link: https://devcentral.f5.com/articles/irules-101-18revisiting-the-tcl-scan-command

    FYI I'm just starting to play with scan so YMMV.

    Sheigh

  • and I also the payload FQDN doesn't have the same length each time (different port, different path, different sub by customers)
  • How about using scan?

    If the variable string is set '9net.tcp://sub.domain.com:12345/abcdefg' or 'sometotherstuff9net.tcp://sub.domain.com:12345/abcdefg/lkjsdflkj/sdfl' etc then try using

    scan $string {%*[^/]//%[^:]} fqdn

    this results in a new variable $fqdn of 'sub.domain.com'

    You can test it out by using the tclsh

    % set string "lkasdflkjwe234239net.tcp://sub.domain.com:12345/abcdefg/sdlfkjasf"
    lkasdflkjwe234239net.tcp://sub.domain.com:12345/abcdefg/sdlfkjasf
    % puts $string
    lkasdflkjwe234239net.tcp://sub.domain.com:12345/abcdefg/sdlfkjasf
    
    % scan $string {%*[^/]//%[^:]} fqdn
    1
    % puts $fqdn
    sub.domain.com
    

    Check out this link: https://devcentral.f5.com/articles/irules-101-18revisiting-the-tcl-scan-command

    FYI I'm just starting to play with scan so YMMV.

    Sheigh

  • Here's what I found.

     

    Tells me if it's optimize or not.

     

    set TCP_Payload [string range [TCP::payload] [expr [string first "://" [TCP::payload]] + 3] [expr [string last ":" [TCP::payload]] - 1]]

     

    I must use an expr cause the string first and string last return only the first index.

     

    Any other suggestion would be appreciated

     

  • Both works

     

    mine : set TCP_Payload [string range [TCP::payload] [expr [string first "://" [TCP::payload]] + 3] [expr [string last ":" [TCP::payload]] - 1]]

     

    Sheigh : scan [TCP::payload] {%*[^/]//%[^:]} TCP_Payload

     

    does the scan use regexp ? I heard that regexp is more cpu intensive.

     

    I want to know which on is less CPU intensive ?

     

    Thanks Sheigh for your output

     

    • Sheigh_65772's avatar
      Sheigh_65772
      Icon for Cirrus rankCirrus
      From my reading scan is faster than regex: https://devcentral.f5.com/s/articles/so-yeah-regex-is-bad
  • Maybe I'm missing what your trying to do.

    Here's another scan example:

    % scan $string {%*[^:]://%[^:]} fqdn
    1
    % puts $fqdn
    sub.domain.com
    

    This will exclude everything up until '://' which it will also throw out. From there it will match the remaining string until the next instance of ':' is found and put that into the fqdn variable.

  • You didn't miss anything.

     

    Both solution works. I just want to know which one is less cpu intensive.

     

    Thanks

     

  • Quick time comparison of the two:

    % time {  set fqdn [string range $string [expr [string first "://" $string] + 3] [expr [string last ":" $string] - 1]] } 10000
    5.7755 microseconds per iteration
    % time { scan $string {%*[^:]://%[^:]} fqdn } 10000
    1.2681 microseconds per iteration
    
    • netadmindetail_'s avatar
      netadmindetail_
      Icon for Nimbostratus rankNimbostratus
      Wow, thank you very much for this kind of output. Can you tell me how you've been able to grab this information ? Is this the time keyword ? how do you stop the timer ?
    • Sheigh_65772's avatar
      Sheigh_65772
      Icon for Cirrus rankCirrus
      So that was all from within the tclsh shell. From your f5 device just type tclsh and you'll drop in. The time command just takes what you want to run and runs it the number of times you specify: time { your commands } itterations