3 Converts netscreen snoop hex-dumps to a hex-dump that text2pcap can read.
5 Copyright (c) 2004 by Gilbert Ramirez <gram@alumni.rice.edu>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 def __init__(self, name, base_time):
33 self.fh = open(name, "w")
37 self.base_time = base_time
38 self.prev_timestamp = 0.0
40 def PrintPacket(self, timestamp, datalines):
41 # What do to with the timestamp? I need more data about what
42 # the netscreen timestamp is, then I can generate one for the text file.
43 # print "TS:", timestamp.group("time")
45 timestamp = float(timestamp.group("time"))
47 sys.exit("Unable to convert '%s' to floating point." % \
50 # Did we wrap around the timeer max?
51 if timestamp < self.prev_timestamp:
52 self.base_time += self.TIMER_MAX
54 self.prev_timestamp = timestamp
56 packet_timestamp = self.base_time + timestamp
58 # Determine the time string to print
59 gmtime = time.gmtime(packet_timestamp)
60 subsecs = packet_timestamp - int(packet_timestamp)
62 subsecs = int(subsecs * 10)
64 print >> self.fh, "%s.%d" % (time.strftime("%Y-%m-%d %H:%M:%S", gmtime), \
67 # Print the packet data
69 for lineno, hexgroup in datalines:
70 hexline = hexgroup.group("hex")
71 hexpairs = hexline.split()
72 print >> self.fh, "%08x %s" % (offset, hexline)
73 offset += len(hexpairs)
78 re_timestamp = re.compile(r"^(?P<time>\d+\.\d): \d+\((?P<io>.)\):")
79 re_hex_line = re.compile(r"(?P<hex>([0-9a-f]{2} ){1,16})\s+(?P<ascii>.){1,16}")
81 def run(input_filename, output_filename):
83 ifh = open(input_filename, "r")
88 ctime = os.stat(input_filename)[stat.ST_CTIME]
92 output_file = OutputFile(output_filename, ctime)
98 for line in ifh.xreadlines():
101 m = re_timestamp.search(line)
106 m = re_hex_line.search(line)
108 datalines.append((lineno, m))
111 output_file.PrintPacket(timestamp, datalines)
116 output_file.PrintPacket(timestamp, datalines)
122 print >> sys.stderr, "Usage: netscreen2dump.py netscreen-dump-file new-dump-file"
126 if len(sys.argv) != 3:
129 run(sys.argv[1], sys.argv[2])
131 if __name__ == "__main__":