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
0.0.7.255 $
255.255.248.0
mask-cvt.pl n b
/21 $
255.255.248.0
mask-cvt.pl w n
255.252.0.0 $
0.3.255.255
mask-cvt.pl w b
/14 $
0.3.255.255
mask-cvt.pl b n
255.255.248.0 $
/21
mask-cvt.pl b w
0.0.7.255
/21
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]; ...
Get Cisco IOS Cookbook, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.