Skip to content

Instantly share code, notes, and snippets.

@sulrich
Created January 11, 2013 16:17
Show Gist options
  • Save sulrich/4511900 to your computer and use it in GitHub Desktop.
Save sulrich/4511900 to your computer and use it in GitHub Desktop.
some spirent test center releases have a bug where they encode traffic stream addresses in an, ahem, counterintuitive manner. this unpacks the elements appropriately and generates a merged field with the IP addresses packaged appropriately.
#!/usr/bin/perl
# -*- mode: cperl; c-basic-offset: 2; indent-tabs-mode: nil -*-
open (INFILE, "<$ARGV[0]") || die "ERROR: opening $ARGV[0]\n";
while (<INFILE>) {
my @traffic = split(',', $_);
next if $#traffic <= 1; # skip the cruft lines in the input file.
my $lt1 = int2addr($traffic[2]);
my $rt1 = int2addr($traffic[3]);
my $addr1 = join('.', $lt1, $rt1);
my $lt2 = int2addr($traffic[4]);
my $rt2 = int2addr($traffic[5]);
my $addr2 = join('.', $lt2, $rt2);
print join(',', @traffic[0,1], $addr1, $addr2, @traffic[6 .. $#traffic]);
}
sub int2addr() {
my ($int) = @_;
# yes, i realize there's probably some (un)pack() l33tness i'm missing here,
# but this is good enough
my $packed_hex = sprintf("%02x", $int);
my $addr = join ('.', hex substr($packed_hex, 0, -2), hex substr($packed_hex, -2));
return $addr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment