Forum Discussion

breizho35_11667's avatar
breizho35_11667
Icon for Nimbostratus rankNimbostratus
Feb 08, 2010

How to read source IP and write http header ?

Hello,

 

 

since commencing in iRules, I seek your help to write a script to meet this need:

 

 

How to read source IP and write http header ?

 

 

when HTTP_Request (

 

 

- Read source IP address (eg 192.168.100.101) and translate this source IP in a digit code number of 10 characters without point (eg 2168100101)

 

- Include a http header corresponding to this digit code (eg TOTO: 1234567890).

 

HTTP::header insert "TOTO" "digit code"

 

 

}

 

 

Please, can you help me to write this script?

 

 

Thanks you for your help,

 

 

Eric

4 Replies

  • Hi Eric,

     

     

    The following is a untested iRule that may work for you

     

     

     

      
         when HTTP_REQUEST {  
           Create the variable that will be used to later to store the IP address with the dots  
           set ipaddr  
             
           Scan command looks at the source address in [IP::client] and breaks it up into 4 variables oct1 to oct4  
           scan [IP::client] {%[^.].%[^.].%[^.].%s} oct1 oct2 oct3 oct4 
             
            append takes all the variables oct1 to oct4 and puts them together into another variable called ipaddr  
           append $ipaddr $oct1 $oct2 $oct3 $oct4  
             
            The command below inserts a header called TOTO with the value of what's in ipaddr  
           [HTTP::header] insert "TOTO" $ipaddr  
         }  
      

     

     

    I have made comments in the code so you understand the logic behind it

     

     

    I hope this helps

     

     

    Bhattman

     

     

     

  • I think the poster is trying to convert the client IP to a decimal value, along the lines of the PHP ip2long function. I was playing around with such a conversion in TCL, but it seems to overflow a 2^32 integer:

     

     

    This works. Returns 2130706433

     

    set ip 127.0.0.1

     

     

    This works. Should return 377299345

     

    set ip 22.125.33.145

     

     

    This does not work. Should return 4211081210

     

    set ip 250.255.255.250

     

     

    scan $ip {%d.%d.%d.%d} a b c d

     

    set dec [expr {[expr {$a <<24}] + [expr {$b <<16}] + [expr {$c <<8}] + $d }]

     

     

    Anyone have suggestions on converting a dotted IP address to decimal format?

     

     

    Aaron
  • Actually, maybe not. Maybe he just wants the periods removed and to take the last 10 characters? If so, something like this should work too:

     
     when CLIENT_ACCEPTED { 
      
         Remove the .'s from the client IP and take the last 10 digits 
        set ip_string [string map {"." ""} [IP::client_addr]] 
        set ip_string [string range $ip_string [expr {[string length $ip_string] - 10}] end] 
     } 
     when HTTP_REQUEST { 
      
         Insert the HTTP header 
        HTTP::header replace TOTO $ip_string 
     } 
     

    Aaron
  • Hello,

     

    thank you very much for your help, I'll try and tell you again.

     

    Best regards,

     

    Eric