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
Hi Don,
I am not a linux shell person but the closest I have gotten to looking at virtuals bigip.conf would be
awk '/(virtual)/,/ }/' bigip.conf
Hopefully someone else who is more proficient could improve this command
Bhattman - hwidjaja_37598
Altostratus
Try using perl, cut and paste this code to a file (eg. /tmp/mygrep.pl) and make sure the executable bit is set (eg. chmod a+x /tmp/mygrep.pl)!/usr/bin/perl use strict; my ($File, $Pattern) = @ARGV; my $buf; open (fh, $File) || die "Cant open $File"; { local $/; $buf=; } close (fh); while ($buf =~ /([^\n]*\{[^\{]*$Pattern[^\}]*\})/sg){ print "$1\n---\n"; }
Sample:[root@bigip:Active] config /tmp/mygrep.pl /config/bigip.conf http-xff 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 } --- [root@bigip:Active] config /tmp/mygrep.pl /config/bigip.conf password user root { password crypt "[cut]" } --- user admin { password crypt "[cut]" description "Admin User" group [cut] home "[cut]" shell "[cut]" role administrator in all } --- user f5emsvr { password crypt "[cut]" description "F5 EM Service Account" id [cut] group [cut] home "[cut]" shell "[cut]" role guest in all } --- configsync { password crypt "[cut]" } ---
It's not well tested, use it at your own risk and on dev machine first. - Jessed12345
Employee
A slight improvement:
awk '/^virtual/,/^}$/' /config/bigip.conf
...now it will only match if 'virtual' appears at the start of the line and end when a '}' appears on a line by itself. I've tested this line with virtuals, pools, moderately complex irules, self-ips, and vlans (using the appropriate keyword in place of 'virtual', obviously). Basically every object that sprang to mind in 30 seconds. Every object was printed correctly, though I suppose it's possible that some portions of the iRules may have been cut off without my noticing.
I also find it a bit easier to use one of these, both of which will let you match on local networking objects (vlans, self-ips) as will as virtuals, pools, etc...:
awk '/^self/,/^}$/' /config/bigip.conf /config/bigip_base.conf
cat /config/{bigip.conf,bigip_base.conf} | awk '/^virtual/,/^}$/'
The creative use of awk variables or bash expansion should allow you to load a bash function that lets you use a simplified syntax, like "list virtual". I have some bash functions to handle exactly that, though they are embarassingly less elegant than this one-liner.
Hope this helps.
--jesse - Don_Munyak
Nimbostratus
Thanks everyone ... much appreciated. I will spend some time testing reults on our dev box.
--Don - Don_Munyak
Nimbostratus
Posted By hwidjaja on 12/28/2009 10:26 AM
!/usr/bin/perl use strict; my ($File, $Pattern) = @ARGV; my $buf; open (fh, $File) || die "Cant open $File"; { local $/; $buf=; } close (fh); while ($buf =~ /([^\n]*\{[^\{]*$Pattern[^\}]*\})/sg){ print "$1\n---\n"; }
Quick question...
For $buf=; ... is correct... or should it be (fh) ??
Thanks again
--Don - hwidjaja_37598
Altostratus
$buf = ; is correct.
You're reading the whole file to a scalar variable ($buf) with fh as the file handle and the < and > around it. - Don_Munyak
Nimbostratus
With respect to awk '/^virtual/,/^}$/' /config/bigip.conf
which prints ALL virtual objects defined.
Is they a way to limit the output, such that, if I have 100 virtuals defined,
but only need the subset where a specific iRule is declared.
I am wondering if there is a way to inject a 'where' clause as a way of limiting the output.
something like where 'rules SNAT2VIP' exists
TIA,
-Don - The_Bhattman
Nimbostratus
Hi Don,
I believe that hwidjaja's perl script addresses that.
I.E.,/tmp/mygrep.pl /config/bigip.conf SNAT2VIP
Bhattman - hwidjaja_37598
Altostratus
hmm try this ...awk 'BEGIN {RS="}";FS=RS} /rules SNAT2VIP/ {print $1"}";} ' /config/bigip.conf
It's the awk one-liner version of the perl script. - Don_Munyak
Nimbostratus
SWEAT ... exactly what I was hoping for.
You rock Humphrey !
-Don
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
