Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
convert_hex_ip_to_human_ip.pl
#!/usr/bin/perl
use strict;
use warnings;
#
# Example usage: tc -s filter show dev bond0| perl convert_hex_ip_to_human_ip.pl
#
while (<>) {
s/([\da-f]{8})/int2ip(hex($1))/ge;
print $_;
}
print int2ip(hex $hex_ip);
sub int2ip {
my $in = shift;
my ($element, $mask, $out);
foreach my $mask (24, 16, 8, 0) {
# mask the current element
my $element = $in & (255 * (2**$mask));
$element = $element / (2**$mask);
if ($element > 255) {
# sanity check
die "$in looks bogus: mask $mask results $element";
}
# glue it on
if ($mask < 24) {
$out = "$out.$element";
} else {
$out = $element;
}
}
# that's it
return $out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.