User guide

CLI Scripting Web Services, CLI Scripting and OpenFlow
page 11-24 OmniSwitch AOS Release 7 Switch Management Guide March 2015
Dest Address Gateway Addr Age Protocol
?------------------+-------------------+----------+-----------
1.1.1.1/32 +10.1.12.1 02:19:54 OSPF
+10.2.12.1 02:19:54 OSPF
+10.3.12.1 02:19:54 OSPF
+10.4.12.1 02:19:54 OSPF
1.1.1.2/32 10.1.22.100 02:19:54 OSPF
1.1.1.3/32 +10.11.23.3 02:19:42 OSPF
+10.12.23.3 02:19:54 OSPF
+10.13.23.3 02:19:54 OSPF
+10.14.23.3 02:19:42 OSPF
1.1.1.4/32 10.1.24.4 02:19:54 OSPF
If we use the grep command we can extract just the first line as in the following example:
-> show ip routes | grep "1.1.1.3/32"
1.1.1.3/32 +10.11.23.3 02:19:42 OSPF
Using awk the command output can be filtered more precisely. The following is a script that would
perform this task:
awk -v pattern="$1" 'BEGIN {
# This will be our flag:
# are we currently reading desired block of info?
INBLOCK = 0
}
{
# Is first field not empty?
# (when it is, number of fields (NF) is just 3)
if (NF == 4) {
# Check whether our string is found in column 3
if ((p = index($0, pattern)) == 3) {
INBLOCK = 1
}
else {
INBLOCK = 0
}
}
# If in block, display line
if (INBLOCK == 1) {
print $0
}
}'
This script can then be easily turned into a standalone shell script by storing it in /flash as filter.sh and
sourcing it using the "." prefix syntax. The script can then be used to filter the output as shown below:
-> show ip routes | . /flash/filter.sh 1.1.1.3/32
1.1.1.3/32 +10.11.23.3 02:19:42 OSPF
+10.12.23.3 02:19:54 OSPF
+10.13.23.3 02:19:54 OSPF
+10.14.23.3 02:19:42 OSPF