#!/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 = <STDIN> ); } # 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]"; }
enjoy!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: IP to hex translator
by Fletch (Bishop) on Aug 14, 2008 at 12:28 UTC | |
by lodin (Hermit) on Aug 14, 2008 at 15:12 UTC | |
|
Re: IP to hex translator
by Anonymous Monk on Aug 14, 2008 at 06:43 UTC | |
|
Re: IP to hex translator
by alexm (Chaplain) on Aug 14, 2008 at 11:29 UTC | |
|
Re: IP to hex translator
by actualize (Monk) on Aug 14, 2008 at 16:08 UTC | |
|
Re: IP to hex translator
by Oleg_T (Initiate) on Sep 18, 2009 at 23:46 UTC | |
|
Re: IP to hex translator
by Oleg_T (Initiate) on Sep 18, 2009 at 23:00 UTC | |
by Oleg_T (Initiate) on Sep 18, 2009 at 23:03 UTC | |
by Oleg_T (Initiate) on Sep 18, 2009 at 23:18 UTC |