CCIE Pursuit Blog

January 9, 2008

Using Regular Expressions With Show Commands

Here’s a nice article showing some interesting uses of regular expressions with show commands.  I’ve posted part of it below, you can read the rest here.   The article looks like it comes from OneTrain, but I couldn’t find it on their site.

*****Updated 10 January*****

David Bombal contacted me and provided the link to the original article (rather than the reposting in the forum):

Hi,

I am glad that you found my article to be useful. This is a copy of an article from my monthly newsletter.

You can find the original article here: http://www.configureterminal.com/newsletters/2.html

Please would you update your links to point to the original article?

Your readers will be able to access more information like this by registering on our website.

Regards,
David Bombal
CCIE #11023, CCSP, CCIP, CCSI, CCNP, CCDP, CCVP
http://www.ConfigureTerminal.com

I have updated the links to point to that article. 

Using the power of regular expressions with Show commands
By David Bombal

This is an advanced topic, so get your ready…

We have covered some basic regular expressions in our “Cool IOS Commands” EBook. Here I want to show you more complicated examples of how to use the power of regular expressions to filter output. This will allow the router to do the searching for text, rather than us doing it manually.

Regular expressions are used in many places in the IOS including BGP AS paths and Voice number translations. They are also used in other languages like Perl and TCL. Here however, we are going to concentrate on regular expressions with IOS show commands. We are going to use them to search for specific sets of strings.

A regular expression is a pattern (for example a phrase or a number) that can be used very effectively to filter output. Regular expressions are case-sensitive and allow for complex matching requirements.

I start with some simple examples so that you can learn each regular expression character individually and then we will combine them into complicated strings. As always with programming, there are many ways to do things, so use your imagination:

^ Regular Expression
Use this to look for text at the beginning of a string.

For Example: ^123 matches 1234, but not 01234 or 91234

On a router we can demonstrate this as follows: (without any regular expressions)

Router#show run | include ip
ip cef
no ip dhcp use vrf connected
ip dhcp pool ITS
option 150 ip 10.1.1.1
no ip domain lookup
voice service voip
allow-connections h323 to sip
allow-connections sip to h323
allow-connections sip to sip
ip address 192.168.10.1 255.255.255.0
ip address 192.168.11.1 255.255.255.0
ip address 192.168.12.1 255.255.255.0
ip address 192.168.13.1 255.255.255.0
ip address 192.168.14.1 255.255.255.0
<MORE>

However, if we use the following:
Router#show run | include ^ip

The output is:
Router#show run | include ^ip
ip cef
ip dhcp pool ITS
ip http server

Note – as expected, every line begins with “ip”, string we matched on
$ Regular Expression:
Use this to look for text at the end of a string

For Example123$ matches 0123, but not 1234

On a router we can demonstrate this as follows: (without any regular expressions)

Router#show run | include 1
Current configuration : 5174 bytes
! Last configuration change at 15:27:21 UTC Wed Jan 24 2007
! NVRAM config last updated at 14:25:01 UTC Wed Jan 24 2007
version 12.4
network 10.1.1.0 255.255.255.0
option 150 ip 10.1.1.1
default-router 10.1.1.1
source-address 10.1.1.1 port 5060
create profile sync 0002381328447096
voice register dn 1
number 1100
number 1101
voice register pool 1
id mac 0003.6B8B.174A
number 1 dn 1
codec g711ulaw
ip address 192.168.10.1 255.255.255.0
interface Loopback1
ip address 192.168.11.1 255.255.255.0
ip address 192.168.12.1 255.255.255.0
ip address 192.168.13.1 255.255.255.0

but if we change it to
Router#show run | include 1$

The output is:
Router#show run | include 1$
voice register dn 1
number 1101
voice register pool 1
number 1 dn 1
interface Loopback1
interface Loopback11
interface Loopback21
interface FastEthernet0/1
session target ipv4:10.1.1.1
session target ipv4:10.1.1.11
session target ipv4:10.1.1.21
session target ipv4:10.1.1.31
session target ipv4:10.1.1.41
session target ipv4:10.1.1.51
session target ipv4:10.1.1.61
number 1001
ephone 1
button 1:1

Note – as expected, every line ends “1”, string we matched on.

Read the rest here.

Advertisements

1 Comment »

  1. Hi,

    I am glad that you found my article to be useful. This is a copy of an article from my monthly newsletter.

    You can find the original article here: http://www.configureterminal.com/newsletters/2.html

    Please would you update your links to point to the original article?

    Your readers will be able to access more information like this by registering on our website.

    Regards,
    David Bombal
    CCIE #11023, CCSP, CCIP, CCSI, CCNP, CCDP, CCVP
    http://www.ConfigureTerminal.com

    Comment by David Bombal — January 10, 2008 @ 2:00 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: