Forum Discussion
Alscion_68122
Nimbostratus
Aug 02, 2011How to use grep in show command
Hi,
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?
Thks
4 Replies
- Mark_Crosland_2Historic F5 AccountOne 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. - Alscion_68122
Nimbostratus
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!
- JRahm
Admin
all the properties on objects should be defined in the tmsh reference guide:
http://support.f5.com/kb/en-us/products/big-ip_ltm/manuals/product/tmsh_reference_guide_10_2_0.html Click Here - Mark_Crosland_2Historic F5 AccountAs I mentioned above, the fields for:
tmsh::get_status
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
tmsh::get_status
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".
Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects