longip perl script

Posted on by admin

I wanted to create a script that would convert a normal IP address to a long IP, just like mIRC Script’s $longip alias.

$longip(address)

Converts an IP address into a long value and vice-versa.

$longip(158.152.50.239)  returns 2660774639

$longip(2660774639)       returns 158.152.50.239

What I was originally trying to do was increase an IP by 1, but due to the octets only allowing up to 255, this became increasingly difficult to do.

What I decided to do in the end was convert the IP to a “longip” then increase it by 1, then convert the IP BACK to normal IP.

This required a way to convert an IP to and from longIP, I was told it could be done purely using shell script, here’s what I did…

I decided that shell script wasn’t powerful enough for what I wanted, and that I could do it easier in perl, this is the result:

#!/usr/bin/perl

# longip by HM2K 2008 (Updated: 17/01/08)

# Description: Converts (Short) IPs to Long Ips and visa versa.
# Usage: ./longip.pl <ip>

use warnings;
use strict;
use Socket;

sub longip {
my $input=shift;
if ($input =~ /\d+\.\d+\.\d+\.\d+/) { return ip2long($input); }
else { return long2ip($input); }
}

sub ip2long { return unpack(“l*”, pack(“l*”, unpack(“N*”, inet_aton(shift)))); }

sub long2ip { return inet_ntoa(pack(“N*”, shift)); }

print longip(shift);

Thanks for the assistance from #perlhelp (EFnet).

It’s also worth noting that cls (EFnet) created a shell script version called “ipconv.sh”, which is about 50 long lines in total (too long for such a simple task imo), however it didn’t convert how I wanted. If you ask him (or me) nicely, you may receive a copy.

Update: I also found a version of “ipconv.sh” in libconnect.

Enjoy!

Related posts:

  1. Seen script for mIRC updated Looking for someone? huh? Well, look no further, this script...
  2. TinySong TCL script for Eggdrop If you’ve not heard of TinySong, you’re missing out! TinySong...
  3. Finding a Portal Script I spent a long time today searching for a PHP...
  4. Storing mySQL database settings for php and perl in one file I have a situation where there’s two scripts. The main...
  5. Google PageRank Script PageRank Scripts on GitHub The Google PageRank is a scoring...

About admin

The author formerly known as HM2K is now the Phurix Labs administrator.

This entry was posted in Development and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>