29.04.2019 Views

OS6860(E)_AOS_8.1.1.R01_Switch_Management_Guide

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CLI Scripting<br />

Web Services, CLI Scripting, and OpenFlow<br />

Dest Address Gateway Addr Age Protocol<br />

?------------------+-------------------+----------+-----------<br />

1.1.1.1/32 +10.1.12.1 02:19:54 OSPF<br />

+10.2.12.1 02:19:54 OSPF<br />

+10.3.12.1 02:19:54 OSPF<br />

+10.4.12.1 02:19:54 OSPF<br />

1.1.1.2/32 10.1.22.100 02:19:54 OSPF<br />

1.1.1.3/32 +10.11.23.3 02:19:42 OSPF<br />

+10.12.23.3 02:19:54 OSPF<br />

+10.13.23.3 02:19:54 OSPF<br />

+10.14.23.3 02:19:42 OSPF<br />

1.1.1.4/32 10.1.24.4 02:19:54 OSPF<br />

If we use the grep command we can extract just the first line as in the following example:<br />

-> show ip routes | grep "1.1.1.3/32"<br />

1.1.1.3/32 +10.11.23.3 02:19:42 OSPF<br />

Using awk the command output can be filtered more precisely. The following is a script that would<br />

perform this task:<br />

awk -v pattern="$1" 'BEGIN {<br />

# This will be our flag:<br />

# are we currently reading desired block of info?<br />

INBLOCK = 0<br />

}<br />

{<br />

# Is first field not empty?<br />

# (when it is, number of fields (NF) is just 3)<br />

if (NF == 4) {<br />

# Check whether our string is found in column 3<br />

if ((p = index($0, pattern)) == 3) {<br />

INBLOCK = 1<br />

}<br />

else {<br />

INBLOCK = 0<br />

}<br />

}<br />

# If in block, display line<br />

if (INBLOCK == 1) {<br />

print $0<br />

}<br />

}'<br />

This script can then be easily turned into a standalone shell script by storing it in /flash as filter.sh and<br />

sourcing it using the "." prefix syntax. The script can then be used to filter the output as shown below:<br />

-> show ip routes | . /flash/filter.sh 1.1.1.3/32<br />

1.1.1.3/32 +10.11.23.3 02:19:42 OSPF<br />

+10.12.23.3 02:19:54 OSPF<br />

+10.13.23.3 02:19:54 OSPF<br />

+10.14.23.3 02:19:42 OSPF<br />

page 10-24 Omni<strong>Switch</strong> <strong>AOS</strong> Release 8 <strong>Switch</strong> <strong>Management</strong> <strong>Guide</strong> May 2014

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!