#!/usr/bin/perl -w use strict; my $first; my $ip; # use the address from @ARGV if it exists or request from STDIN if (@ARGV) { $ip = shift @ARGV; } else { print "Enter an ip address: "; chomp( my $ip = ); } # split the ip address into subnets and store in an array my @subnets = split(/\./, $ip); # ensure the ip address is valid. sanitize(@subnets); for (0..$#subnets) { # take each subnet and generate the hex out of the subnet divided by 16 # then the remainder my $pair = hexize((int($subnets[$_]/16)),($subnets[$_]%16)); print $pair; } print "\n"; sub sanitize { # takes a subnet and makes sure that its not over 255, # greater than 0 and that there are 4 subnets. foreach $_ (@_) { if ($_ > 255) { print "illegal ip address\n"; exit; } elsif ($_ < 0) { print "illegal ip address\n"; exit; } elsif ($#_ != 3) { print "illegal ip address\n"; exit; } } } sub hexize { # Takes two values # 1: the subnet divided by 16 # 2: the subnet divided by 16's remainder # Then it applies the right letter if applicable my @unit; foreach $_ (@_) { if ($_ == 10) { push @unit,"A"; } elsif ($_ == 11) { push @unit,"B"; } elsif ($_ == 12) { push @unit,"C"; } elsif ($_ == 13) { push @unit,"D"; } elsif ($_ == 14) { push @unit,"E"; } elsif ($_ == 15) { push @unit,"F"; } else { push @unit,$_; } } return "$unit[0]$unit[1]"; }