Forum Discussion

Tyler_Brown_949's avatar
Tyler_Brown_949
Icon for Nimbostratus rankNimbostratus
Oct 19, 2015

iRule to Extract and Repsond back with client IP

All,

 I've got the following iRule setup that's being used as a health check for some of our clients.  Right now the text we reply back with is a basic static value "TestOk".  I'm wondering if its possible to something more dynamic where we extract the IP address of the client and respond back with it as the value, but so far I haven't had any luck.  I've looked around here for a similar topic, but haven't found anything yet.

when CLIENT_ACCEPTED { if {[active_members client-443-pool-01] < 1}{ reject } }

when HTTP_REQUEST {

Look for a URI If found send back an HTTP 200 if { [HTTP::uri] equals "/about/health/status" } { Send a 200 response with data HTTP::respond 200 "X-Responce" "TestOk" } else { drop } }

5 Replies

  • Give this a try:

    when HTTP_REQUEST {
         Look for a URI  If found send back an HTTP 200
        if { [HTTP::uri] equals "/about/health/status" } {
             Send a 200 response with data
            HTTP::respond 200 "X-Responce" "TestOk for client [getfield [IP::client_addr] {%} 1]"
        }
        else {
            drop
        }
    }
    
    • Tyler_Brown_949's avatar
      Tyler_Brown_949
      Icon for Nimbostratus rankNimbostratus
      Thanks for the quick response Brad, I'll give that a try. I just got it working with the following as well. when CLIENT_ACCEPTED { if {[active_members client-443-pool-01] < 1}{ reject } } when HTTP_REQUEST { Look for a URI If found send back an HTTP 200 if { [HTTP::uri] equals "/about/health/status" } { Send a 200 response with data HTTP::respond 200 "X-PublicIP" "[IP::client_addr]" "X-Responce" "TestOk" } else { drop } }
  • Give this a try:

    when HTTP_REQUEST {
         Look for a URI  If found send back an HTTP 200
        if { [HTTP::uri] equals "/about/health/status" } {
             Send a 200 response with data
            HTTP::respond 200 "X-Responce" "TestOk for client [getfield [IP::client_addr] {%} 1]"
        }
        else {
            drop
        }
    }
    
    • Tyler_Brown_949's avatar
      Tyler_Brown_949
      Icon for Nimbostratus rankNimbostratus
      Thanks for the quick response Brad, I'll give that a try. I just got it working with the following as well. when CLIENT_ACCEPTED { if {[active_members client-443-pool-01] < 1}{ reject } } when HTTP_REQUEST { Look for a URI If found send back an HTTP 200 if { [HTTP::uri] equals "/about/health/status" } { Send a 200 response with data HTTP::respond 200 "X-PublicIP" "[IP::client_addr]" "X-Responce" "TestOk" } else { drop } }
  • I assume you understand that "X-Response" will be returned to the client as an HTTP header. If you want the data to be present in the HTTP payload you'd use the "Content" tag:

    when HTTP_REQUEST {
        if { [HTTP::uri] equals "/about/health/status" } {
            HTTP::respond 200 Content "TestOK"
        }
    }
    

    As far as the data that you send back, that can come from any values presented in the client's request and potentially a wealth of server side information. For example, to send the client's IP back:

    when HTTP_REQUEST {
        if { [HTTP::uri] equals "/about/health/status" } {
            HTTP::respond 200 X-Response "TestOK: [IP::client_addr]"
        }
    }