Forum Discussion

Alscion_68122's avatar
Icon for Nimbostratus rankNimbostratus
Aug 02, 2011

How to use grep in show command




I want to retrieve the MAC address of all vlan of all partition and stock them in a file.



I would use the show command : show net vlan $part-vlan-virt | grep "Mac" that return the information I need : Mac Address 0:1:d7:ae:xx:xx



But when I use it in a script and try to put the result in a file I have the following error :




Script : puts $file [tmsh::show net vlan vlan-toto | grep "Mac"]



Error : can't eval proc: "script::run"


unexpected argument "|"


while executing


"tmsh::show net vlan vlan-toto | grep "Mac""




Does anybody have an idea or another solution?






4 Replies

  • Mark_Crosland_2's avatar
    Historic F5 Account
    One of the goals for tmsh scripting was to reduce the amount of screen scraping (run command | grep | grep ...) that had to be done.



    There are two commands, tmsh::get_config (list command equivalent) and tmsh::get_status (show command equivalent) that provide structured/programmatic access to config, stats and status. You don't have to use grep. You just need the names of the fields that you are interested in. For tmsh::get_config the field names are what you see in list command output (list ltm virtual). For tmsh::get_status the field names are what you see in show command output with the field-fmt option.



    One of the benefits of these API calls is that they allow you to write a script that will not fail in the future when we add something to the output of "sh net vlan" that happens to have the string "Mac" in it.



    If you issue this command you will see the mac-true field.


    tmsh show net vlan field-fmt



    You can then write scripts that access the fields you see in the output of "show field-fmt".



    Here is a script that displays the mac of each vlan.



    cli script vlan_macs {


    proc script::run {} {


    foreach obj [tmsh::get_status /net vlan] {


    puts "[tmsh::get_name $obj] [tmsh::get_field_value $obj mac-true]"









    tmsh run cli script vlan_macs



    There is more API info here.


    help cli cript



    That being said, I took a look at the source code and we do in fact ignore the pipe character in script mode. We will revisit that decision to determine if | should be allowed in a script.


  • Hoooooo, excellent, I used to use only the command we find under tmsh. All these get_command seems to be powerful and will simplifiy and proper my scipts!!



    Is there a document or reference where can I find all [field name] values? (mac-true etc...)




    Thanks a lot!


  • all the properties on objects should be defined in the tmsh reference guide:


  Click Here
  • Mark_Crosland_2's avatar
    Historic F5 Account
    As I mentioned above, the fields for:





    Are the fields that you see in list command, and as Jason pointed out, are also in the tmsh reference guide.


    Example: tmsh list ltm pool





    Are the fields that you see in the show command with the field-fmt option


    Example: tmsh show ltm pool field-fmt


    Unfortunately F5 has never really documented that statistic and status fields in the CLI, and (head hanging in shame) we did not start to do that with tmsh either.


    But they are available via "tmsh show ... field-fmt".