#!/usr/bin/perl
 
use strict;
use warnings;
 
our $help = "
 Usage: $0 <sniffer dump file> 
 Option - (dash) read from stdin
 ex. ssh -l admin \<fortigate ip or fqdn\> \"diag sniffer packet any 'ip' 3 0 l\" | $0 - | text2pcap - - | wireshark -k -i -
\n";
 
our $sdump;
our ($input) = $ARGV[0];
 
if (not defined $input) {
  die "$help\n";
}
 
if ( -e $input ) {
        open  ($sdump, qq(\x3C), $input);
} elsif ( $input =~ /^-$/ ) {
        $sdump = *STDIN;
} else {
        die "\nError in open dump $input may not exist or unable to read!\n $help";
}
 
while (<$sdump>) {
        if ( /^\d+-\d+-\d+\s+\d+:\d+:\d+\.\d+/ ) {
                our ($date, $time) = /^(\d+-\d+-\d+)\s+(\d+:\d+:\d+\.\d+)\s+/;
                print $date . qq(\x20) . $time . "\n";
        } elsif ( /^$/ ) {
                print;
        } elsif ( /^0x/ ) {
                s/0x/00/;
                s/^(\w+)\s+(.*)\t(.*)/$1 $2/;
                our ($hex, $x1, $x2, $x3, $x4, $x5, $x6, $x7, $x8, $null) = split ' ';
                print $hex . '  ';
                foreach our $out ( $x1, $x2, $x3, $x4, $x5, $x6, $x7, $x8 ) {
                        if ( $out ) {
                                substr $out, 2, 0, qq(\x20);
                                print $out . qq(\x20);
                        }
                }
                print "\n";
        }
}