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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 # Find a timestamp line
79 re_timestamp = re.compile(r"^(?P<time>\d+\.\d): [\w/]+\((?P<io>.)\)(:| len=)")
81 # Find a hex dump line
82 re_hex_line = re.compile(r"(?P<hex>([0-9a-f]{2} ){1,16})\s+(?P<ascii>.){1,16}")
84 def run(input_filename, output_filename):
86 ifh = open(input_filename, "r")
90 # Get the file's creation time.
92 ctime = os.stat(input_filename)[stat.ST_CTIME]
96 output_file = OutputFile(output_filename, ctime)
102 for line in ifh.xreadlines():
104 # If we have no timestamp yet, look for one
106 m = re_timestamp.search(line)
110 # Otherwise, look for hex dump lines
112 m = re_hex_line.search(line)
114 datalines.append((lineno, m))
116 # If we have been gathering hex dump lines,
117 # and this line is not a hex dump line, then the hex dump
118 # has finished, and so has the packet. So print the packet
119 # and reset our variables so we can look for the next packet.
121 output_file.PrintPacket(timestamp, datalines)
125 # At the end of the file we may still have hex dump data in memory.
126 # If so, print the packet
128 output_file.PrintPacket(timestamp, datalines)
134 print >> sys.stderr, "Usage: netscreen2dump.py netscreen-dump-file new-dump-file"
138 if len(sys.argv) != 3:
141 run(sys.argv[1], sys.argv[2])
143 if __name__ == "__main__":