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.
$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:
- Storing mySQL database settings for php and perl in one file I have a situation where there’s two scripts. The main...
- Seen script for mIRC updated Looking for someone? huh? Well, look no further, this script...
- Ident Spoofing Today I wrote about Oidentd on Wikipedia. Ident as per...