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 w0.0.7.255 $255.255.248.0mask-cvt.pl n b/21 $255.255.248.0mask-cvt.pl w n255.252.0.0 $0.3.255.255mask-cvt.pl w b/14 $0.3.255.255mask-cvt.pl b n255.255.248.0 $/21mask-cvt.pl b w0.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]; ...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.
Read now
Unlock full access