Forum Discussion
don_23889
Nimbostratus
Dec 28, 2009grep'ng for 'string' in bigip.conf
grep'ng for 'string' and returning entire block of object definitions from bigip.conf for each instance of 'string', starting with 'virtual' and ending with '}'
config less bigip.conf |grep -B5 -A5 string |more
This is cumbersome since you have to guess for -B(x) and -A(x)
So, as an example, I want to return all object definitions with iRule 'http-xff'. Basically, all lines between [virtual] and [}]
This example is syntactically incorrect, and only for illustrating what I am trying to achieve....ie...pseudo-code.
less bigip.conf |grep BEGIN[virtual] END[}] http-xff |more
virtual vip1.80 {
pool vip1.80
destination aaa.bbb.ccc.ddd:http
ip protocol tcp
rules SNAT2VIP
profiles
http-xff
tcp-lan-optimized
serverside
tcp-wan-optimized
clientside
persist source_addr_30min
}
virtual vip-2.80 {
pool vip-2.80
destination mmm.nnn.ooo.ppp:http
ip protocol tcp
rules SNAT2VIP
profiles
http-xff
tcp-lan-optimized
serverside
tcp-wan-optimized
clientside
persist source_addr_30min
}
What would really be sweet, would be to grep for a given 'string', and return entire block for 'any' object definition...where the preceding line and following line are are both blank. This would return all object definitions for a given string...virtual, pool, monitor...etc.
30 Replies
- The_Bhattman
Nimbostratus
You have to love this type of posts where it evolves.
Bhattman - Jessed12345
Employee
I have been inspired. Building off the pretty much every other poster in this thread, here's my not-exactly-a-one-liner solution:list() { object=$1 pattern=$2 if [ -z "$2" ]; then awk '/'^"$object"'/,/^}$/ {print}' /config/bigip{.conf,_base.conf} else for i in $(b $object list | awk 'BEGIN {RS="\n}"} /'"$pattern"'/ {print $2}'); do awk '/'^"$object $i"'/,/^}$/ {print}' /config/bigip{.conf,_base.conf} done fi }
Put that at the bottom of the '/root/.bash_profile' file, then either logout and back in, or source the file with:source /root/.bash_profile
After that you can either search for all objects of a certain type with:
list
Or you can search for the set of objects that contain a specific pattern with:list
Your original example would work with:list virtual http-xff
This isn't nearly as fast as Humphrey's perl script, and certainly not as fast as a good awk one-liner, but it's flexible, and it will work with patterns that are contained within an embedded block ({...}) in the object. One example would be trying to find all SSL vips that use a specific clientside profile, or all iRules that use a certain pool within an if/else, etc... I've tested it a bit, but I may have missed some condition that causes it to fail. If you catch something that I didn't, please let me know.
If your worried about losing your .bash_profile during an upgrade, you can copy this to a file and put that file in /shared/bin (the 'bin' sub-dir does not exist by default). Then add this to the bottom of your .bash_profile:source /path/to/file
Actually, I keep all funtions like this in a file located in /shared/bin, then source that file from .bash_profile so everything is available on login. If I boot to the other partition all I need to do to get everything back is re-add the 'source' command to .bash_profile.
PS> I tried -a lot- to isolate the object name from within a single awk statement rather than resorting to calling bigpipe... it will take someone better than I to make it happen.
--jesse - The_Bhattman
Nimbostratus
Hi Jesse,
Great work!!!
Bhattman - hoolio
Cirrostratus
I think these examples might break with an iRule which has a closing brace on an empty line within the iRule code. Many iRules in the bigip.conf have un-indented lines for the event definitions:rule rule1 { when RULE_INIT { blah blah } <--------- when HTTP_REQUEST { if (blahblah1) { blah blah ... if (blahblah2) { blah blah ... } } else { blah blah ... } } <--------- }
It would be ideal if you could actually check for the closing curly brace that corresponds to the object definition--not just the first closing brace alone on a line (\n}\n). But I'm not sure how to do this in awk or perl.
Aaron - hwidjaja_37598
Altostratus
Thanks Aaron ... This code will check the closing curly brace that corresponds to an object.!/usr/bin/perl my ($File, $Pattern) = @ARGV; my ($CurlyBraces, $Object) = (0, ""); open (fh, $File) || die "Cant open $File"; while ( ){ $Object .= $_; $CurlyBraces += (tr/\{//); $CurlyBraces -= (tr/\}//); if ($CurlyBraces == 0) { print $Object if ($Object =~ /$Pattern/s); $Object = ""; } } close (fh); - JRahm
Admin
This should be a cakewalk with the new scripting ability in tmsh. I'll send a $25 amazon gift card to the first non-F5 user who figures it out and posts here. Take a look in the tmsh wiki (Click here) for an assist. - JRahm
Admin
No takers, eh? I'll post a solution Monday, so you have until then. - hwidjaja_37598
Altostratus
I'm interested to try but I don't have v10.1 ready right now - hwidjaja_37598
Altostratus
What about this, my first tmsh scriptproc script::run {} { if { $tmsh::argc != 3 } { puts "Args: , e.g., virtual http_xff" } else { foreach {obj} [tmsh::get_config /ltm [lindex $tmsh::argv 1] ] { foreach {field_name} [tmsh::get_field_names value $obj] { if { [tmsh::get_field_value $obj $field_name] contains [lindex $tmsh::argv 2]} { puts "[tmsh::list [tmsh::get_type $obj] [tmsh::get_name $obj]]" break } } } } }
sample:root@bigip(Active)(tmos) run cli script abc2 virtual http_xff ltm virtual http_xff { destination x.x.x.x:https ip-protocol tcp mask x.x.x.x pool http_xff profiles { tcp { } } snat automap } - JRahm
Admin
Posted the solution yesterday to the wiki...forgot to pass along.
http://devcentral.f5.com/Wiki/default.aspx/tmsh/ConfigSearch.html Click here
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)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
