Hello everyone!
My name is Capri Gomez, and I was invited to the blog by Mr.Ronelus who taught Science when I was in PS.33. I graduated in 2000 and have since been studying Computers.
Today I'd like to share a segment of code with you all. It is written in Perl, which is a pretty friendly language for doing all sorts of tasks. This segment is used in one of my tools to convert an IP Address (A numerical network identifier for computers), into its hexadecimal (Base 16) equivalent. An IP Version 4 address is made up of 4 octets (8 bit segments), which when combined form a 32 bit number. IP addresses are actually Base 2 numbers (Binary). So essentially what we are doing is converting Base 2 to Base 16.
This model was created by the University of Hawaii in the 60's and was never intended to be used at the grandiose scale of the Internet. For this reason, IPv6 is currently in the process of implementation, and uses 128-bit numbers, rendering many more permutations (2^128 instead of 2^32!). In the current example, the IP 192.168.1.113 is converted to C0A80171.
You can modify the $ip variable and run this script on any Unix/Linux system (or even Windows), as long as Perl is installed. Keep in mind that valid values for each octet may only be between 0 and 254, to be valid on a standard network.
In order to save the file, copy and paste the code into a new text file, and save it as a .pl. You may have to apply execution permissions (chmod +x) in order to actually run the code.
Automation via programming is something which most people don't make use of, but you can use it to execute long tedious tasks in seconds instead of hours. Imagine the possiblities of shaving YEARS of work away, leaving more time for anything you want! It takes some time to learn, but it is definitely worthwhile.
Lines which start with "#" are comments, which means the Perl interpreter ignores them. They are there to aid the reader in understanding the code. The first line is an exception, as scripts must declare a path to the actual interpreter.
#!/usr/bin/perl -w
# Use -w or -d --^ for warnings/debug
# Capri Gomez
# Mystic Solutions
use Socket;
use v5.10;
sub ip2hex {
# Input IP Address (Modify this Value)
$ip = "192.168.1.113";
# Split IP into its Octets
($oct1, $oct2, $oct3, $oct4) = split(/\./, $ip, 4);
# Array for work.
@ipa = ($oct1, $oct2, $oct3, $oct4);
$hexipa = "";
# Format each array into hexadecimal and build hex IP.
foreach(@ipa) {
# Debug Output
#print $_;
$pll = $_;
# Check that IP matches valid range
if (($pll >= 0) && ($pll < 255)) {
# The IF is a check for a numeric as well as a valid value.
} else { die "Check your Terminal IP...\n"; }
# Calculate first column (x16)
$part1 = $pll / 16;
# Format to represent integer.
$part1 = sprintf("%d", $part1);
# Calculate second column (x1)
$part2 = $pll % 16;
# Array for work
@columns = ($part1,$part2);
# Print both for debug
#print $part1 . ":" . $part2 . "\n";
# Assign Number or Letter to hex string
foreach(@columns){
given($_) {
when (/10/) {
#print "This number is an A:$_\n";
$hexipa = $hexipa . "A";
}
when (/11/) {
#print "This number is a B:$_\n";
$hexipa = $hexipa . "B";
}
when (/12/) {
#print "This number is a C:$_\n";
$hexipa = $hexipa . "C";
}
when (/13/) {
#print "This number is a D:$_\n";
$hexipa = $hexipa . "D";
}
when (/14/) {
#print "This number is an E:$_\n";
$hexipa = $hexipa . "E";
}
when (/15/) {
#print "This number is an F:$_\n";
$hexipa = $hexipa . "F";
}
default {
#print "This number is a digit:$_\n"
$hexipa = $hexipa . $_;
}
}
}
}
print $hexipa . "\n";
return $hexipa;
}
&ip2hex();
## EOF
hi i am sayed gomez i am iterested in this capri
ReplyDeleteI'm glad to hear it Sayed. I have more code on a hard drive which is currently out of my reach, but I have Programming examples in Perl, PHP, C.... You'll be hearing more from me!
DeleteHi i am also a blogger but i am new i hope you come to visit and Mr.r is a great teacher he teaches every one and i think i an going to past the test because of him i am glad he is a science teacher.
ReplyDeleteand as you see my name is Karen
ReplyDeleteHi Karen,
DeleteChances are I will be visiting on Friday, the 3rd of May. I'm looking forward to meeting everyone!