IP Conversion..Sending to Subroutine..

J

Joe Henderson

All,

When i can to send the dec value of an ip add to a sub it doesn't work..

Is there any othe way i can do this w/o substring it..?

Or does ne1 have a better way...

###############

my $ip = "10.10.1.1";
my @octet = split(/\./, $ip); #Like to remove this garbage.., Just send the
whole string..
my $dec = &decimal(@octet);
my $dec1 = substr($dec, 0, 5); #Like to remove this garbage..
my $dec2 = substr($dec, 5, 5); #Like to remove this garbage..
my $ip = &ip($dec1, $dec2);

sub decimal
{
my @octet = @_;
my $oct1 = $octet[0];
my $oct2 = $octet[1];
my $oct3 = $octet[2];
my $oct4 = $octet[3];
my $dec1 = $oct1 * (256**3);
my $dec2 = $oct2 * (256**2);
my $dec3 = $oct3 * 256;
my $dec4 = $oct4;
my $decimal = $dec1 + $dec2 + $dec3 + $dec4;
return $decimal;
}


sub ip
{
my @dec = @_;
my $dec1 = $dec[0];
my $dec2 = $dec[1];
my $decimal = "$dec1$dec2"; #join back together
$decimal =~s/^\s+//; #strip all leading whitespace
$decimal =~s/\s+$//; #strip all trailing whitespace

$oct1 = ($decimal / 256**3);
@num = split(/\./, $oct1);
$oct1 = $num[0];
$dec1 = ($oct1 * 256**3);
$decimal = $decimal - $dec1;

if($decimal >= 256**2)
{
$oct2 = ($decimal / 256**2);
@num = split(/\./, $oct2);
$oct2 = $num[0];
$dec2 = ($oct2 * 256**2);
$decimal = $decimal - $dec2;
}

if($decimal >= 256)
{
$oct3 = ($decimal / 256);
@num = split(/\./, $oct3);
$oct3 = $num[0];
$dec3 = $oct3 * 256;
$decimal = $decimal - $dec3;
}

$oct4 = $decimal;
$oct4 = $decimal;
if($oct1 < 1) { $oct1 = 0; }
if($oct2 < 1) { $oct2 = 0; }
if($oct3 < 1) { $oct3 = 0; }
if($oct4 < 1) { $oct4 = 0; }
$ip = "$oct1.$oct2.$oct3.$oct4";
return $ip;
}

###############


TIA,

Joe
 
J

Jeff 'japhy' Pinyan

Or does ne1 have a better way...

I don't really know what you're doing, or why you're doing it.
my $ip = "10.10.1.1";
my @octet = split(/\./, $ip); #Like to remove this garbage.., Just send the
my $dec = &decimal(@octet);

Then why not send the whole string and have the function do the splitting?
my $dec1 = substr($dec, 0, 5); #Like to remove this garbage..
my $dec2 = substr($dec, 5, 5); #Like to remove this garbage..
my $ip = &ip($dec1, $dec2);

Why do you want to split the value into two pieces, only to rejoin them
later?!
 
J

Joe

I don't really know what you're doing, or why you're doing it.


Normally this is a whole array of sorting ip's in a file..
..While reading a file of ip's i want to sort then by lowest
to highest then print.. However.. when i do not split them
the values are not correctly push to the sub's.. So.. I tried
to use "big::int" but that did not work.. Any Ideas?
 
J

John W. Krahn

Joe said:
When i can to send the dec value of an ip add to a sub it doesn't work..

Is there any othe way i can do this w/o substring it..?

Or does ne1 have a better way...

Perhaps you want the inet_aton() and inet_ntoa() functions from the
Socket module.

perldoc Socket


John
 
D

David Efflandt

Normally this is a whole array of sorting ip's in a file..
.While reading a file of ip's i want to sort then by lowest
to highest then print.. However.. when i do not split them
the values are not correctly push to the sub's.. So.. I tried
to use "big::int" but that did not work.. Any Ideas?

This is an example of a script that can sort a file list of full or
partial IPs, or insert (optional) single IP from the commandline into the
sorted list.

Simply sorting a unique list of packed IPs and then unpacking them did
not work for partial IPs because either 192.168. or 192.168.0. became
192.168.0.0 which would not work for intended purpose. So I put the IPs
into a hash with a packed value, and sort on the value string (cmp). Not
sure if this is optimized, but it works.

The partial IPs (ending with dot) are for class A, B, or C subnets (dot is
appended if needed). I did not get into more specific subnet masking yet
(more complicated). This demo does not check if IPs are valid and would
need added code for multi-user access of same IP list (flock, etc.).

#!/usr/bin/perl -w
# banip 5/30/2003 by David Efflandt (e-mail address removed)
# Inserts (optional) single full or partial IP from commandline
# into file list. List is sorted and duplicates removed.
#
# set $file to point to your IP list file
my $file = "$ENV{HOME}/banlist.txt";
my $newip = shift or 0;
$newip =~ s/^[^\d\.]+$// if $newip;
my ($iplist,%hash);
if ($_ = $newip) {
$newip .= '.' unless (/\.$/ || /\d+\.\d+\.\d+\.\d+/);
$hash{$newip} = pack('C4',split(/\./,$newip));
print "using: $file\n";
} else {
warn "usage: $0 ip_addr\n$file is just being sorted\n";
}
if (open (IN,"$file")) {
while (<IN>) {
chomp;
$_ .= '.' unless (/\.$/ || /\d+\.\d+\.\d+\.\d+/);
$hash{$_} = pack('C4',split(/\./,$_));
}
close IN;
} elsif (!$newip) {
die "nothing to do!\n";
} else {
die "can't read $file: $!\n";
}
@iplist = sort { $hash{$a} cmp $hash{$b} } keys %hash;
open (OUT,"> $file") || die "Can't write $file: $!";
foreach (@iplist) {
print OUT "$_\n";
}
close OUT;
 
J

Joe

David Efflandt said:
This is an example of a script that can sort a file list of full or
partial IPs, or insert (optional) single IP from the commandline into the
sorted list.

Yes.. Correct
Simply sorting a unique list of packed IPs and then unpacking them did
not work for partial IPs because either 192.168. or 192.168.0. became
192.168.0.0 which would not work for intended purpose. So I put the IPs
into a hash with a packed value, and sort on the value string (cmp). Not
sure if this is optimized, but it works.

The partial IPs (ending with dot) are for class A, B, or C subnets (dot is
appended if needed). I did not get into more specific subnet masking yet
(more complicated). This demo does not check if IPs are valid and would
need added code for multi-user access of same IP list (flock, etc.).

#!/usr/bin/perl -w
# banip 5/30/2003 by David Efflandt (e-mail address removed)
# Inserts (optional) single full or partial IP from commandline
# into file list. List is sorted and duplicates removed.
#
# set $file to point to your IP list file
my $file = "$ENV{HOME}/banlist.txt";
my $newip = shift or 0;
$newip =~ s/^[^\d\.]+$// if $newip; This is a new one..
my ($iplist,%hash);
if ($_ = $newip) {
$newip .= '.' unless (/\.$/ || /\d+\.\d+\.\d+\.\d+/);
$hash{$newip} = pack('C4',split(/\./,$newip));
need to read up on this "pack();"
print "using: $file\n";
} else {
warn "usage: $0 ip_addr\n$file is just being sorted\n";
}
if (open (IN,"$file")) {
while (<IN>) {
chomp;
$_ .= '.' unless (/\.$/ || /\d+\.\d+\.\d+\.\d+/);
$hash{$_} = pack('C4',split(/\./,$_));
}
close IN;
} elsif (!$newip) {
die "nothing to do!\n";
} else {
die "can't read $file: $!\n";
}
@iplist = sort { $hash{$a} cmp $hash{$b} } keys %hash;
open (OUT,"> $file") || die "Can't write $file: $!";
foreach (@iplist) {
print OUT "$_\n";
hmm.. wonder what happen to the 2nd-4th octet.. doesn't this just print the
1st?
}
close OUT;

Hey.. Good stuff tho..thanks for a different look..

Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top