#!/usr/bin/perl
$sCMD = "ifconfig";
foreach $argnum (0 .. $#ARGV) {
$sCMD = "$sCMD $ARGV[$argnum]";
}
$string = `$sCMD`;
print batch_hex_ip_to_dec($string);
# batch_hex_ip_to_dec(string)
# string: string to search and replace ips for
# returns: string with ip in dec format
sub batch_hex_ip_to_dec
{
my $string = shift;
my @arr = split /(0x\w{8})/ig, $string;
for (my $i = 0; $i < @arr; $i++) {
if ($arr[$i] =~ /0x\w{8}/i) {
$arr[$i] = hex_ip_to_dec($arr[$i]);
}
}
return join('', @arr);
}
# hex_ip_to_dec(ip)
# ip: string, hex format, eg 0xffffff00
# returns: string, dec format, eg 255.255.255.0
sub hex_ip_to_dec
{
my $ip = shift;
my $r= '';
my @vals = ($ip =~ /0x(\w{2})(\w{2})(\w{2})(\w{2})/i);
for (my $i = 0; $i < @vals; $i++) {
$r .= hex_to_dec($vals[$i]);
unless ($i == @vals - 1) { $r .= '.'; }
}
return $r;
}
# hex_to_dec(number)
# number: hex number to convert
# returns: decimal value
sub hex_to_dec
{
return sprintf "%d", hex(shift);
}