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]

Perl Madness

So I get paid overtime and it’s a pain to work out how much I’m owed each week at 1.5x and 2.0x so I wrote a Perl script to do it.
Screenshot_2017-08-25_12-42-51.png
Can’t figure out what is going on with the warning about line 95. Here is the source code:
[code language=”perl”]
#!/usr/bin/perl
=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
use warnings;
use strict;
use Scalar::Util qw(looks_like_number);
my @work_days = qw / Monday Tuesday Wednesday Thursday Friday /;
our $start;
our $finish;
our $leave = 0;
our $total = 0;
our $dbltime;
our $timehalf;
our $std_day = 7.5;
our $hrs = 0;
our $currentday;
our $daylen;
foreach (@work_days) {
while (!valid($_)) {
#print "$start $finish looping \n";
}
$currentday = $_;
if (($start eq 0) && ($finish eq 0)) {
$hrs = 0;
calchrs();
} else {
$hrs = $finish – $start -1;
calchrs();
}
#print "Total hrs: $total hrs: $hrs Sick Leave: $leave \n";
}
sub valid {
my $line;
my $line2;
print "$_ start time: ";
while (chomp($line = <STDIN>), !looks_like_number($line)) {
print "\n";
print "Please enter a number: ";
}
$start = $line;
print "$_ finish time: ";
while (chomp($line2 = <STDIN>), !looks_like_number($line2)) {
print "\n";
print "Please enter a number: ";
}
$finish = $line2;
if (($start >= $finish) || ($start <= 0) || ($finish > 24)) {
print "Error. Invalid inputs.\n";
return 0;
} else {
return 1;
}
}
sub calchrs {
if ($currentday eq "Friday") {
$daylen = 8;
} else {
$daylen = $std_day;
}
if ($hrs > ($daylen + 3)) {
$total += $daylen;
$timehalf += 3;
$dbltime += ($hrs – ($daylen +3));
} elsif ($hrs > $daylen) {
$total += $daylen;
$timehalf += $hrs – $daylen;
} elsif ($hrs == $daylen) {
$total += $hrs;
} elsif ($hrs < $daylen) {
our $line3 = 0;
$hrs += 1;
print "Enter lunch break: ";
while (chomp($line3 = <STDIN>), !looks_like_number($line3), ($line3 gt $hrs)) {
print "\n";
print "error enter valid number: ";
}
$hrs -= $line3;
$leave += $daylen – $hrs;
$total += $hrs;
}
}
print " Total hrs: $total\n Total 1.5x: $timehalf\n Total 2.0x: $dbltime\n Leave: $leave\n";
[/code]
BTW Perl is a real pleasure to program in. The biggest hang up was cause by Perl comparing numbers to
strings. I finally found a function called looks_like_number which helped solved the issue of accepting
letters and symbols as inputs.
Did I mention I love Perl?

Virtual Nightmare

So I have concluded the OSPF part of my study for now and so far ospfd has been working just fine on OpenBSD 6.1 – that is up until I use the “default-information originate” command on a Cisco router to enable a default ¬†route to the external internet.
I have made a new setup to test DR router election and diagnose this ospf default route problem.
 
DeepinScreenshot_select-area_20170819041414.png
 
The host machine (FreeBSD amd64 with a Ryzen 1700) runs an Archlinux VM inside Virtual Box.
This then runs GNS3 with runs OpenBSD inside QEMU and the Cisco IOS images inside Dynamips. Yuck!
I know but I refuse to run Linux on my main machine and it is powerful enough to allocate 8GB of RAM to and 4 cores.
I have chosen IP addresses which will not conflict with my Host’s Host IP network (10.0.0.0/24).
OSPF is configured on all routers and R1 is elected as the DR. All works fine until the “default-information originate” command is entered.
The “route show” command on OpenBSD is frozen.
A “route flush” command shows the potential issue:
DeepinScreenshot_select-area_20170819044504.png
As you can see the entry “10.137.0/24” is an incorrect or incomplete IPv4 address. I have tried multiple setups and multiple IP addresses and this continues to happen.
If I disable the “default-information originate” command the routing table is restored immediately but I still cannot get connectivity even with static routes!
DeepinScreenshot_select-area_20170819050015.png
I am starting to wonder whether it is an issue with OpenBSD 6.1 as the first routing entry is missing an octet.
What makes it hard is pings to external interfaces are not supported in VirtualBox even tho the interface has been set to enable promiscuous mode.
The “route show” command works when OpenBSD is acting as the default ospf gateway:
DeepinScreenshot_select-area_20170819053151.png
It’s simple to do this just add “redistribute default” and add the interface.
DeepinScreenshot_select-area_20170819053427.png
All Cisco routers pickup the routes successfully:
DeepinScreenshot_select-area_20170819053033.png
Anyway I give up for the time being. I have wasted a few hours on it already with no avail.
 

OpenBSD's ospfd with Cisco Routers


I am currently studying for my CCNA and I am at the OSPF part. While on holidays and reading through the OpenBSD man pages I noticed that OpenBSD comes with various routing protocols in the base system.
So I created a VM running OpenBSD 6.1 in QEMU available here to experiment with OSPF using the base installation configuration (no other packages required or installed) to experiment with it.
Here is the topology:
deepin-screenshot-fullscreen
As you can see in the terminal window when R1’s link to OBSDR-1 goes down a path to R1 via R2 is automatically selected. The convergence time was around 10ms! Not too bad at all.
Here are the configuration details for the OBSDR-1 box (OpenBSD QEMU machine):

Please not the the interface em2 is missing a “passive” sub-command.
You can use the ospfctl command to show information on the FIB and RIB:

The Cisco routers have a basic config here is R1 (most content omitted):
[code language=”perl”]
interface GigabitEthernet1/0
ip address 10.0.0.1 255.255.255.252
negotiation auto
!
interface GigabitEthernet2/0
ip address 10.0.0.5 255.255.255.252
negotiation auto
!
interface GigabitEthernet6/0
ip address 10.1.1.100 255.255.255.0
negotiation auto
!
router ospf 1
router-id 1.1.1.1
log-adjacency-changes
passive-interface GigabitEthernet6/0
network 10.0.0.0 0.0.0.3 area 0
network 10.0.0.4 0.0.0.3 area 0
network 10.1.1.0 0.0.0.255 area 0
!
[/code]

Here is R2:

[code language=”perl”]
interface GigabitEthernet1/0
ip address 10.0.0.9 255.255.255.252
negotiation auto
!
interface GigabitEthernet2/0
ip address 10.0.0.6 255.255.255.252
negotiation auto
!
interface GigabitEthernet6/0
ip address 10.1.2.100 255.255.255.0
negotiation auto
!
router ospf 1
router-id 2.2.2.2
log-adjacency-changes
passive-interface GigabitEthernet6/0
network 10.0.0.4 0.0.0.3 area 0
network 10.0.0.8 0.0.0.3 area 0
network 10.1.2.0 0.0.0.255 area 0
!
[/code]
Simple! I noticed when the timers of the Cisco routers and OpenBSD box were different the ospf daemon wouldn’t start. So I adjusted the hello timer to 10.
Be sure to check /var/log/messages if /etc/rc.d/ospfd won’t start.
A simple ‘halt -p’ on the OpenBSD box followed by a start command for the OpenBSD router inside GNS3 is all that is needed to bring the machine back up after configuring it for the first time.
Here are some links to software/products used:
GNS3
OpenBSD
ospf man page
ospf.conf man page
Cisco C7200 Router