December 2006
Intermediate to advanced
1188 pages
72h 8m
English
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.
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]; ...