Learning Subnets Masks Using Perl

I need to learn IPv4 CIDR and decimal subnet masks for my CCNA exam. I wrote a quick and dirty Perl script to help me do that.
I want to add a count down timer to make it more interesting. But I have run out of time. I will have to try that at a later date.
[code language=”perl”]
use strict;
use warnings;
=for comment
Copyright <2017> <Wilyarti Howard>
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=cut
$|++;
my $i = 0;
my @asked;
my %subnets = (
31 => { addresses => "2", netmask => "255.255.255.254"},
30 => { addresses => "4", netmask => "255.255.255.252"},
29 => { addresses => "8", netmask => "255.255.255.248"},
28 => { addresses => "16", netmask => "255.255.255.240"},
27 => { addresses => "32", netmask => "255.255.255.224"},
26 => { addresses => "64", netmask => "255.255.255.192"},
25 => { addresses => "128", netmask => "255.255.255.128"},
24 => { addresses => "256", netmask => "255.255.255.0"},
23 => { addresses => "512", netmask => "255.255.254.0"},
22 => { addresses => "1024", netmask => "255.255.252.0"},
21 => { addresses => "2048", netmask => "255.255.248.0"},
20 => { addresses => "4096", netmask => "255.255.240.0"},
19 => { addresses => "8192", netmask => "255.255.224.0"},
18 => { addresses => "16384", netmask => "255.255.192.0"},
17 => { addresses => "32768", netmask => "255.255.128.0"},
16 => { addresses => "65536", netmask => "255.255.0.0"},
15 => { addresses => "131072", netmask => "255.254.0.0"},
14 => { addresses => "262144", netmask => "255.252.0.0"},
13 => { addresses => "524288", netmask => "255.248.0.0"},
12 => { addresses => "1048576", netmask => "255.240.0.0"},
11 => { addresses => "2097152", netmask => "255.224.0.0"},
10 => { addresses => "4194304", netmask => "255.192.0.0"},
9 => { addresses => "8388608", netmask => "255.128.0.0"},
8 => { addresses => "16777216", netmask => "255.0.0.0"},
);
print "
————————————————————————
Subnet Game – Copyright 2017 Wilyarti Howard
Released under the 2 Clause BSD License.
Welcome to the subnet practise game!
A Classless Interdomain Routing prefix (CIDR) is a common way to represe
nt a subnet mask.
To calculate the mask first start counting from the left to right. Each
octet in a subnet mask consists of a 8 bit binary number.
There are 4 octets in a subnet mask.
Each number in the CIDR represents a binary \"1\" in the subnet mask:
For example: /16 = 11111111.11111111.00000000.00000000
To calculate the subnet mask simply add each row from left to right.
A binary one in each row means the following number:
Row values:\t128\t64\t32\t16\t8\t4\t2\t1\n
Total value:\t128\t192\t224\t240\t248\t252\t254\t255\n\n
For example: /18
11111111.11111111.00000000.0000000 <- /16 (first two octets)
11111111.11111111.11000000.00000000 <- /18
Now 11000000 has 1 in the 128 column and 1 in the 64 column.
128 + 64 = 192
So the decimal equivalent is 255.255.128.0 (255 = 11111111)
";
while( $i < 14)
{
my $random_number = int(rand(16)) + 14;
while ( $random_number ~~ @asked) {
$random_number = int(rand(16)) + 14;
}
print "What is CIDR /" . $random_number . " in decimal?: ";
my $answer;
chomp($answer = <STDIN>);
my $solution = $subnets{$random_number}{netmask};
if ($solution eq $answer) {
print "Correct answer!\n";
$i++;
push (@asked, $random_number);
} else {
print "Error wrong answer! The answer = $solution \n";
;
}
}
[/code]

Leave a comment

Your email address will not be published. Required fields are marked *