Forum Discussion

Michaelyang's avatar
Michaelyang
Icon for Cirrostratus rankCirrostratus
Mar 17, 2023

Problem about Export imformation to Excel

Hello,

I have made some changes by referring to the following URL:
https://community.f5.com/t5/codeshare/export-virtual-server-configuration-in-csv-tmsh-cli-script/ta-p/281983
This is my script :

proc script::run {} {
        puts "VServer_name,VServer_IP:Port,Protocol,Load_Balance,Sticky_Method,AP_Server_Name,AP_Server_IP:Port,Monitor_Protocol"
        foreach all_partitions [tmsh::get_config auth partition] {
            set partition "[lindex [split $all_partitions " "] 2]"
            tmsh::cd /$partition
            foreach { obj } [tmsh::get_config ltm virtual all-properties] {
                set poolname [tmsh::get_field_value $obj "pool"]
                set profiles [tmsh::get_field_value $obj "ip-protocol"]i
                set persist [lindex [lindex [tmsh::get_field_value $obj "persist"] 0] 1]
                if { $poolname != "none" }{
                    set poolconfig [tmsh::get_config /ltm pool $poolname all-properties]
                    set poolconfig2 [tmsh::get_config /ltm pool $poolname]
                    foreach poolinfo $poolconfig poolinfo2 $poolconfig2 {
                        set pool_lb [tmsh::get_field_value $poolinfo "load-balancing-mode"]
                        set pool_monitor [tmsh::get_field_value $poolinfo "monitor"]
                        if { [catch { set member_name [tmsh::get_field_value $poolinfo "members" ]} err] } {
                            set pool_member $err
                            puts "[tmsh::get_name $obj],[tmsh::get_field_value $obj "destination"],$profiles,$pool_lb,$persist,$poolname,$pool_member,$pool_monitor"
                        } else {
                            set pool_member ""
                            set member_name [tmsh::get_field_value $poolinfo2 "members" ]
                            foreach member $member_name {
                                append pool_member " ([lindex [lindex $member 2] 1][lindex $member 1] )"
                            }
                            puts "[tmsh::get_name $obj],[tmsh::get_field_value $obj "destination"],$profiles,$pool_lb,$persist,$poolname,$pool_member,$pool_monitor"
                        }
                    }
                } else {
                    puts "[tmsh::get_name $obj],[tmsh::get_field_value $obj "destination"],$profiles,none,$persist,$poolname,none,none,none"
                }
            }
        }
    }
}

About the column AP_Server_IP:Port, I want to change it to IP:Port (142.251.43.4:443), but no matter what I try, I can't do it.

By the way,Is there any way to display only ports in numeric format, e.g. 80, 443, 8080,... ? (not http,https,FTP,...)

Any help is appreciate.

 

  • i modify and optimize the tmsh script, it reports pool_name, pool_members_list(members is addr:port or addr.port), the disadvantage is when use [tmsh::get_status /ltm virtual $VS_name], it will lose the route domain-id, such as %1,%2 and so on.

    script support AS3 and Common or other partition

    cli script virtual-details {
        proc script::init {} {
        }
        proc check_vs {partition {partition_folder ""}} {
            if { $partition_folder eq "" } {
                tmsh::cd /$partition
            } else {
                tmsh::cd /${partition}/${partition_folder}
            }
            foreach { obj } [tmsh::get_config ltm virtual all-properties] {
                set VS_name [tmsh::get_name $obj]
                set VSname ""
                foreach { VSip } [tmsh::get_status /ltm virtual $VS_name] {
                    set VSIIP [tmsh::get_field_value $VSip "destination"]
                }
                set poolname [tmsh::get_field_value $obj "pool"]
                set profiles [tmsh::get_field_value $obj "ip-protocol"]
                set persist [lindex [lindex [tmsh::get_field_value $obj "persist"] 0] 1]
                if { $poolname != "none" } {
                    set poolconfig [tmsh::get_config /ltm pool $poolname all-properties]
                    foreach { poolinfo } $poolconfig {
                        set pool_lb [tmsh::get_field_value $poolinfo "load-balancing-mode"]
                        set pool_monitor [tmsh::get_field_value $poolinfo "monitor"]
                        if { [catch { set member_name [tmsh::get_field_value $poolinfo "members" ]} err] } {
                            set pool_member $err
                            puts "$VS_name,$VSIIP,$profiles,$pool_lb,$persist,$poolname,$addr,$port,$pool_monitor"
                        } else {
                            set pool_member {}
                            foreach mem [tmsh::get_status /ltm pool $poolname members] {
                                foreach poolmember [tmsh::get_field_value $mem "members"] {
                                    set addr [tmsh::get_field_value $poolmember "addr"]
                                    set port [tmsh::get_field_value $poolmember "port"]
                                    if { $addr contains "." } {
                                        lappend pool_member "$addr:$port"
                                    } else {
                                        lappend pool_member "$addr.$port"
                                    }
                                }
                            }
                            # puts "--------"
                            # puts "$pool_member"
                            # puts "$VS_name,$VSIIP,$profiles,$pool_lb,$persist,$poolname,$pool_member,$pool_monitor"
                        }
                    }
                    puts "$VS_name,$VSIIP,$profiles,$pool_lb,$persist,$poolname,$pool_member,$pool_monitor"
                } else {
                    puts "$VS_name,$VSIIP,$profiles,none,$persist,$poolname,none,none"
                }
            }
        }
        proc script::run {} {
            puts "VServer_name,VServer_IP:Port,Protocol,Load_Balance,Persist_Method,PooL_Name,PooL_Members,Monitor_Protocol"
            foreach all_partitions [tmsh::get_config auth partition] {
                # set partition "[lindex [split $all_partitions " "] 2]"
                set partition "[tmsh::get_name ${all_partitions}]"
                tmsh::cd /$partition
                if { [tmsh::get_config /sys folder] eq "" } {
                    check_vs $partition
                } else {
                    foreach { partition_folder_config } [tmsh::get_config /sys folder] {
                        set partition_folder_name [tmsh::get_name ${partition_folder_config}]
                        if { $partition eq "Common" } {
                            check_vs $partition
                            break
                        } else {
                            check_vs ${partition} ${partition_folder_name}
                        }
                    }
                }  
            }
        }
        proc script::help {} {
        }
        proc script::tabc {} {
        }
    }

     

  • xuwen's avatar
    xuwen
    Icon for Cumulonimbus rankCumulonimbus

    tmsh::get_config ltm pool equals tmsh command "list ltm pool xxx", so some special protocol ports will become http, https, and so on.

    "show ltm pool xxx field-fmt members" will return poolmember addr and port

    tmsh script command is 

     

    proc script::run {} {
        set poolname "pool_test"
        foreach { mem } [tmsh::get_status /ltm pool $poolname members] {
            foreach poolmember [tmsh::get_field_value $mem "members"] {
                set addr [tmsh::get_field_value $poolmember "addr"]
                set port [tmsh::get_field_value $poolmember "port"]
                puts "poolmember name is [lindex $poolmember 1]"
                puts "poolmember addr is $addr"
                puts "poolmember port is $port"
                puts "---------"
            }
        }
    }

     

    run cli script output is:

     

    root@(f5)(cfg-sync Standalone)(Active)(/Common)(tmos)# run cli script extract_poolmembers 
    poolmember name is 192.168.200.3:80
    poolmember addr is 192.168.200.3
    poolmember port is 80
    ---------
    poolmember name is app_server1:80
    poolmember addr is 192.168.200.1
    poolmember port is 80
    ---------
    poolmember name is app_server2:80
    poolmember addr is 192.168.200.2
    poolmember port is 80
    ---------

     

    There are limitations to using tmsh script:

    1. if your ltm pool is create by AS3, you need to cd /partition/foldername cause not easy to extract information

    2.  use python api is easy to extract information 

    so the best easy way is to use python f5 api to extract ltm pool members ip and port attribute

    • Michaelyang's avatar
      Michaelyang
      Icon for Cirrostratus rankCirrostratus

      Hi xuwen ,

      Thanks for your help.
      I have modified the script as you suggested and it shows :80 :443 without any problems, but it only shows the last setting, my script is below.

      modify script virtual-details {
      proc script::run {} {
              puts "VServer_name,VServer_IP:Port,Protocol,Load_Balance,Sticky_Method,AP_Server_Name,AP_Server_IP,AP_Server_Port,Monitor_Protocol"
              foreach all_partitions [tmsh::get_config auth partition] {
                  set partition "[lindex [split $all_partitions " "] 2]"
                  tmsh::cd /$partition
                  foreach { obj } [tmsh::get_config ltm virtual all-properties] {
                      set VS_name [tmsh::get_name $obj]
                  }
                  set VSname ""
                  foreach { VSip } [tmsh::get_status /ltm virtual $VS_name] {
                      set VSIIP [tmsh::get_field_value $VSip "destination"]
                  }
                      set poolname [tmsh::get_field_value $obj "pool"]
                      set profiles [tmsh::get_field_value $obj "ip-protocol"]
                      set persist [lindex [lindex [tmsh::get_field_value $obj "persist"] 0] 1]
                      if { $poolname != "none" }{
                          set poolconfig [tmsh::get_config /ltm pool $poolname all-properties]
                          set poolconfig2 [tmsh::get_config /ltm pool $poolname]
                          foreach poolinfo $poolconfig poolinfo2 $poolconfig2 {
                              set pool_lb [tmsh::get_field_value $poolinfo "load-balancing-mode"]
                              set pool_monitor [tmsh::get_field_value $poolinfo "monitor"]
                              if { [catch { set member_name [tmsh::get_field_value $poolinfo "members" ]} err] } {
                                  set pool_member $err
                                  puts "$VS_name,$VSIIP,$profiles,$pool_lb,$persist,$poolname,$addr,$port,$pool_monitor"
                              } else {
                                  set pool_member ""
                                  foreach mem [tmsh::get_status /ltm pool $poolname members] {
                                      foreach poolmember [tmsh::get_field_value $mem "members"] {
                                          set addr [tmsh::get_field_value $poolmember "addr"]
                                          set port [tmsh::get_field_value $poolmember "port"]
                                      }
                                  }
                                  puts "$VS_name,$VSIIP,$profiles,$pool_lb,$persist,$poolname,$addr,$port,$pool_monitor"
                              }
                          }
                      } else {
                          puts "$VS_name,$VSIIP,$profiles,none,$persist,$poolname,none,none,none,none"
                      }
                  }
              }
      }

      Any help is appreciate.

       

  • xuwen's avatar
    xuwen
    Icon for Cumulonimbus rankCumulonimbus

    i modify and optimize the tmsh script, it reports pool_name, pool_members_list(members is addr:port or addr.port), the disadvantage is when use [tmsh::get_status /ltm virtual $VS_name], it will lose the route domain-id, such as %1,%2 and so on.

    script support AS3 and Common or other partition

    cli script virtual-details {
        proc script::init {} {
        }
        proc check_vs {partition {partition_folder ""}} {
            if { $partition_folder eq "" } {
                tmsh::cd /$partition
            } else {
                tmsh::cd /${partition}/${partition_folder}
            }
            foreach { obj } [tmsh::get_config ltm virtual all-properties] {
                set VS_name [tmsh::get_name $obj]
                set VSname ""
                foreach { VSip } [tmsh::get_status /ltm virtual $VS_name] {
                    set VSIIP [tmsh::get_field_value $VSip "destination"]
                }
                set poolname [tmsh::get_field_value $obj "pool"]
                set profiles [tmsh::get_field_value $obj "ip-protocol"]
                set persist [lindex [lindex [tmsh::get_field_value $obj "persist"] 0] 1]
                if { $poolname != "none" } {
                    set poolconfig [tmsh::get_config /ltm pool $poolname all-properties]
                    foreach { poolinfo } $poolconfig {
                        set pool_lb [tmsh::get_field_value $poolinfo "load-balancing-mode"]
                        set pool_monitor [tmsh::get_field_value $poolinfo "monitor"]
                        if { [catch { set member_name [tmsh::get_field_value $poolinfo "members" ]} err] } {
                            set pool_member $err
                            puts "$VS_name,$VSIIP,$profiles,$pool_lb,$persist,$poolname,$addr,$port,$pool_monitor"
                        } else {
                            set pool_member {}
                            foreach mem [tmsh::get_status /ltm pool $poolname members] {
                                foreach poolmember [tmsh::get_field_value $mem "members"] {
                                    set addr [tmsh::get_field_value $poolmember "addr"]
                                    set port [tmsh::get_field_value $poolmember "port"]
                                    if { $addr contains "." } {
                                        lappend pool_member "$addr:$port"
                                    } else {
                                        lappend pool_member "$addr.$port"
                                    }
                                }
                            }
                            # puts "--------"
                            # puts "$pool_member"
                            # puts "$VS_name,$VSIIP,$profiles,$pool_lb,$persist,$poolname,$pool_member,$pool_monitor"
                        }
                    }
                    puts "$VS_name,$VSIIP,$profiles,$pool_lb,$persist,$poolname,$pool_member,$pool_monitor"
                } else {
                    puts "$VS_name,$VSIIP,$profiles,none,$persist,$poolname,none,none"
                }
            }
        }
        proc script::run {} {
            puts "VServer_name,VServer_IP:Port,Protocol,Load_Balance,Persist_Method,PooL_Name,PooL_Members,Monitor_Protocol"
            foreach all_partitions [tmsh::get_config auth partition] {
                # set partition "[lindex [split $all_partitions " "] 2]"
                set partition "[tmsh::get_name ${all_partitions}]"
                tmsh::cd /$partition
                if { [tmsh::get_config /sys folder] eq "" } {
                    check_vs $partition
                } else {
                    foreach { partition_folder_config } [tmsh::get_config /sys folder] {
                        set partition_folder_name [tmsh::get_name ${partition_folder_config}]
                        if { $partition eq "Common" } {
                            check_vs $partition
                            break
                        } else {
                            check_vs ${partition} ${partition_folder_name}
                        }
                    }
                }  
            }
        }
        proc script::help {} {
        }
        proc script::tabc {} {
        }
    }

     

  • Hi xuwen ,

    Thanks for your help.
    I have since tried again and the following script runs the way I want it to.

    modify script virtual-details {
    proc script::run {} {
            puts "VServer_name,VServer_IP:Port,Protocol,Load_Balance,Sticky_Method,AP_Server_Name,AP_Server_IP:Port,Monitor_Protocol"
            foreach all_partitions [tmsh::get_config auth partition] {
                set partition "[lindex [split $all_partitions " "] 2]"
                tmsh::cd /$partition
                foreach { obj } [tmsh::get_config ltm virtual all-properties] {
                    set VS_name [tmsh::get_name $obj]
                    set VSname ""
                    foreach { VSip } [tmsh::get_status /ltm virtual $VS_name] {
                        set VSIIP [tmsh::get_field_value $VSip "destination"]
                    }
                    set poolname [tmsh::get_field_value $obj "pool"]
                    set profiles [tmsh::get_field_value $obj "ip-protocol"]
                    set persist [lindex [lindex [tmsh::get_field_value $obj "persist"] 0] 1]
                    if { $poolname != "none" }{
                        set poolconfig [tmsh::get_config /ltm pool $poolname all-properties]
                        set poolconfig3 [tmsh::get_config /ltm pool $poolname]
                        set poolconfig2 [tmsh::get_status /ltm pool $poolname members]
                        foreach poolinfo $poolconfig poolinfo2 $poolconfig2 {
                            set pool_lb [tmsh::get_field_value $poolinfo "load-balancing-mode"]
                            set pool_monitor [tmsh::get_field_value $poolinfo "monitor"]
                            if { [catch { set member_name [tmsh::get_field_value $poolinfo "members" ]} err] } {
                                set pool_member $err
                                puts "$VS_name,$VSIIP,$profiles,$pool_lb,$persist,$poolname,$pool_member,$pool_monitor"
                            } else {
                                set pool_member ""
                                set member_name [tmsh::get_field_value $poolinfo2 "members" ]
                                foreach member $member_name {
                                    append pool_member " ([lindex [lindex $member 2] 1]:[lindex [lindex $member 2] 39]) "
                                }
                                puts "$VS_name,$VSIIP,$profiles,$pool_lb,$persist,$poolname,$pool_member,$pool_monitor"
                            }
                        }
                    } else {
                        puts "$VS_name,$VSIIP,$profiles,none,$persist,$poolname,none,none,none,none"
                    }
                }
            }
    }
    }

    Thanks for letting me learn a few things