longip perl script

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


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

$longip(  returns 2660774639

$longip(2660774639)       returns

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:


# 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.