Skip to Content
Cisco IOS Cookbook, 2nd Edition
book

Cisco IOS Cookbook, 2nd Edition

by Kevin Dooley, Ian Brown
December 2006
Intermediate to advanced
1188 pages
72h 8m
English
O'Reilly Media, Inc.
Content preview from Cisco IOS Cookbook, 2nd Edition

Converting Different Mask Formats

Problem

You want to convert between the three different formats that Cisco routers use for presenting mask information: standard netmask, ACL wildcards, and CIDR bit number format.

Solution

The following Perl script converts from any of these formats—netmask, wildcard, or bit count—to any other. The usage syntax is mask-cvt {n|w|b} {n|w|b} {nnn.nnn.nnn.nnn|/bits}, where the first argument specifies what the input format is and the second argument specifies the output format. In both cases, “n” is for netmask format, “w” is for wildcard format, and “b” is for CIDR bit format (with or without the leading slash, as in /24).

For example:

$ mask-cvt.pl n w 255.255.248.0
0.0.7.255
$ mask-cvt.pl n b 255.255.248.0
/21
$ mask-cvt.pl w n 0.3.255.255
255.252.0.0
$ mask-cvt.pl w b 0.3.255.255
/14
$ mask-cvt.pl b n /21
255.255.248.0
$ mask-cvt.pl b w /21
0.0.7.255

The Perl code follows in Example 5-1.

Example 5-1. mask-cvt.pl

#!bin/perl # # mask-cvt.pl -- a script to convert between the various # methods of masking IP addresses # sub usage() { print "mask-cvt [nwb] [nwb] {nnn.nnn.nnn.nnn|bbb}\n"; print " where the first argument, [nwba], specifies the input \n"; print " format as one of netmask, wildcard or number of \n"; print " bits and the second argument, [nwb], specifies \n"; print " the output format\n"; exit(); } if($#ARGV != 2) { usage(); } # get the input format style $_ = @ARGV[0]; if(/[nN]/) { # incoming format netmask, what's the outgoing $_ = @ARGV[1]; ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

IP Routing on Cisco IOS, IOS XE, and IOS XR: An Essential Guide to Understanding and Implementing IP Routing Protocols

IP Routing on Cisco IOS, IOS XE, and IOS XR: An Essential Guide to Understanding and Implementing IP Routing Protocols

Brad Edgeworth, Aaron Foss, Ramiro Garza Rios
Cisco Software-Defined Access

Cisco Software-Defined Access

Srilatha Vemula, Jason Gooley, Roddie Hasan

Publisher Resources

ISBN: 0596527225Supplemental ContentErrata Page