How iRule get Destination IP of Virtual server?

Hi gurus,

How could I get the Destination IP of Virtual server from iRule? Thanks

Phong

Can you expand on the question ?

I’m not quite sure I understand the requirements…

Example: 1 have 3 VSs:

  • VS_A has IP 10.0.0.11
  • VS_B has IP 10.0.0.12
  • VS_C has IP 10.0.0.13 and iRule RL_C

When Client access to VS_C, iRule will run and redirect Client to VS_A or VS_B. But I don’t want to put IP address of VS_A and VS_B. I want to put VS name and iRule will get the IP address.

Thanks

Do you want a redirect ? or do you want to use VS_A based upon a certain condition?

What’s your current iRule ?

Why connect to VS_C in the first place ? :slight_smile:

you can use:

when CLIENT_ACCEPPTED {

virt A

}

Thank Iain. I didn’t have iRule now. I just want to know how to get the VS IP if I have the VS name.

Phong

Hi Phong,

there is no build-in command available to resolve a given Virtual Server name to its IP address. But you could write an iCall script to periodically enumerate your LTM configuration and create/update an internal data-group with Key=VSName and Value=VSIP.

Note: I’ve published an iCall script on CodeShare that created a data-group to resolve Node_IPs to Node_Names. The code can be very easily recycled to resolve VS_Name to VS_IP. Let me know if you have troubles to modify the iCall script as needed…

Cheers, Kai

I don’t beleive you can dynamically query the configuration of another virtual server outside of the one the iRule is currently attached to.

If your goal is to redirect the user to a different virtual, you can route control to a different virtual with the “virtual [name]” command.

-Joe

Hi Phong,

below are the required steps to setup the iCall handler to create and periodically update a data-group containing your Virtual Server Name and their corresponding IPs…

1.) SSH to your LTM and enter TMSH

2.) Issue the command: load sys config merge from-terminal

3.) Copy and Paste the code below…

Note: You may want to tweak the interval of the iCall handler as needed…

sys icall script DataGroup_Virtual_to_IP {
    app-service none
    definition {
        set virtual_list ""
        set virtual_counter 0
         tmsh::log "iCall: Starting to enumerate existing node objects..."
        foreach partition [tmsh::get_config auth partition] {
            set partition "/[tmsh::get_name $partition]"
             tmsh::log "iCall: Processing Partition: $partition"
            tmsh::cd $partition
            foreach virtual [tmsh::get_config /ltm virtual] {
                 tmsh::log "Processing Node : $partition/[tmsh::get_name $virtual]"
                set destination [lindex [split [tmsh::get_field_value $virtual "destination"] ":"] 0]
                set destination [lindex [split $destination "%"] 0]
                append virtual_list "\"[tmsh::get_name $virtual]\" \{ data \"$destination\" \}\n"
                incr virtual_counter   
            }
             tmsh::log "Finished Partition: $partition"
        }
        tmsh::cd "/Common"
        if { not ([tmsh::list /ltm data-group] contains "ltm data-group internal DG_VS_2_IP") } then {
            tmsh::log "iCall: Created the data-group \"DG_VS_2_IP\"."
            tmsh::create /ltm data-group internal "DG_VS_2_IP" type "string"
        } else {
             tmsh::log "iCall: The DataGroup does exist."
        }
        eval "tmsh::modify /ltm data-group internal DG_VS_2_IP \{ records replace-all-with \{ $virtual_list \} \}"
        tmsh::log "iCall: Updated the data-group DG_VS_2_IP with \"$virtual_counter\" entries."
    }
    description none
    events none
}
sys icall handler periodic DataGroup_Virtual_to_IP {
    first-occurrence 2016-09-27:00:00:00
    interval 60
    script DataGroup_Virtual_to_IP
}

4.) Hit CTRL+D to apply the config merge.

5.) After a short period, take a look to your LTM logfile and see the resulting log output.

6.) Verify that the data-group “DG_VS_2_IP” is successfully created.

7.) Write an iRule and see if it works…

when RULE_INIT {
    log local0.debug "IP Address of Virtual Server \"VS_A\" is \"[class match -value "VS_A" equals DG_VS_2_IP]\""
}

8.) Verify once again the log output.

Cheers, Kai