Forum Discussion

Deon's avatar
Deon
Icon for Nimbostratus rankNimbostratus
Jan 06, 2009

How To Concatenate Strings

OK, Newbie here. Trying to figure out how to concatenate strings properly in an iRule. Ive got the following code and the newaddr value is not being formatted like I expect.

 

 

  when HTTP_RESPONSE {    
     set addr [IP::server_addr]   
     log local0. $addr   
     set c [getfield $addr "." 3]   
     set d [getfield $addr "." 4]   
     set newaddr [concat "1.1." $c "." $d]   
     log local0. $newaddr   
     HTTP::header insert "Server_ID" [IP::server_addr]   
   }

 

 

The intent of the iRule is to insert a somewhat obfuscated IP address of the member server as a header tag in the response. So, if the member server IP is 10.44.6.11, the desired tag value inserted would be "1.1.6.11". The result I am getting is "1.1. 6 . 11 ". How do I strip out the spaces around the vars c and d? Is there a better or more desirable way to do this than the code I've got here?

 

 

Thanks much!

 

-Deon

2 Replies

  • Hi Deon,

    concat (Click here) will "join each of its arguments together with spaces after trimming leading and trailing white-space from each of them.". You can just combine variables though:

    set combined "$var1$var2"

    Instead of using getfield twice, you could use scan once:

     
     when RULE_INIT { 
      
        set addr [IP::server_addr]  
        set addr "111.222.333.444" 
        log local0. "Original \$addr: $addr" 
      
         Scan the address for four digits separated by periods 
        scan $addr {%d.%d.%d.%d} a b c d 
      
         Take the third and fourth octets and combine them with the static first two octets. 
        set newaddr "1.1.$c.$d" 
        log local0. "scan result: $newaddr"    
     } 
     

    Log output:

    Rule : Original $addr: 111.222.333.444

    Rule : scan result: 1.1.333.444

    Aaron
  • Deon's avatar
    Deon
    Icon for Nimbostratus rankNimbostratus
    Excellent. I had a feeling I was taking a long route when trying to do the concatenation. Thanks much for your help!

     

     

    -Deon