]>
Commit | Line | Data |
---|---|---|
af51e6f0 | 1 | #! /usr/bin/env @PYTHON@ |
7aa697dd | 2 | # |
e0edde6f | 3 | # Copyright (c) 2010 Nicira, Inc. |
7aa697dd BP |
4 | # |
5 | # Licensed under the Apache License, Version 2.0 (the "License"); | |
6 | # you may not use this file except in compliance with the License. | |
7 | # You may obtain a copy of the License at: | |
8 | # | |
9 | # http://www.apache.org/licenses/LICENSE-2.0 | |
10 | # | |
11 | # Unless required by applicable law or agreed to in writing, software | |
12 | # distributed under the License is distributed on an "AS IS" BASIS, | |
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | # See the License for the specific language governing permissions and | |
15 | # limitations under the License. | |
16 | ||
8ea171ab RB |
17 | from __future__ import print_function |
18 | ||
7aa697dd BP |
19 | import binascii |
20 | import getopt | |
21 | import struct | |
22 | import sys | |
23 | ||
bdca6c4b | 24 | |
7aa697dd BP |
25 | class PcapException(Exception): |
26 | pass | |
27 | ||
bdca6c4b | 28 | |
7aa697dd BP |
29 | class PcapReader(object): |
30 | def __init__(self, file_name): | |
31 | self.file = open(file_name, "rb") | |
32 | header = self.file.read(24) | |
33 | if len(header) != 24: | |
34 | raise PcapException("end of file reading pcap header") | |
35 | magic, version, thiszone, sigfigs, snaplen, network = \ | |
36 | struct.unpack(">6I", header) | |
37 | if magic == 0xa1b2c3d4: | |
38 | self.header_format = ">4I" | |
39 | elif magic == 0xd4c3b2a1: | |
40 | self.header_format = "<4I" | |
41 | else: | |
42 | raise PcapException("bad magic %u reading pcap file " | |
43 | "(expected 0xa1b2c3d4 or 0xd4c3b2a1)" % magic) | |
44 | ||
45 | def read(self): | |
46 | header = self.file.read(16) | |
47 | if len(header) == 0: | |
48 | return None | |
49 | elif len(header) != 16: | |
50 | raise PcapException("end of file within pcap record header") | |
51 | ||
52 | ts_sec, ts_usec, incl_len, orig_len = struct.unpack(self.header_format, | |
53 | header) | |
54 | packet = self.file.read(incl_len) | |
55 | if len(packet) != incl_len: | |
56 | raise PcapException("end of file reading pcap packet data") | |
57 | return packet | |
81a86b9a JS |
58 | |
59 | ||
7aa697dd BP |
60 | argv0 = sys.argv[0] |
61 | ||
bdca6c4b | 62 | |
7aa697dd | 63 | def usage(): |
8ea171ab | 64 | print("""\ |
7aa697dd BP |
65 | %(argv0)s: print pcap file packet data as hex |
66 | usage: %(argv0)s FILE | |
67 | where FILE is a PCAP file. | |
68 | ||
69 | The following options are also available: | |
70 | -h, --help display this help message | |
71 | -V, --version display version information\ | |
8ea171ab | 72 | """ % {'argv0': argv0}) |
7aa697dd BP |
73 | sys.exit(0) |
74 | ||
81a86b9a | 75 | |
7aa697dd BP |
76 | if __name__ == "__main__": |
77 | try: | |
78 | try: | |
79 | options, args = getopt.gnu_getopt(sys.argv[1:], 'hV', | |
80 | ['help', 'version']) | |
f3068bff | 81 | except getopt.GetoptException as geo: |
7aa697dd BP |
82 | sys.stderr.write("%s: %s\n" % (argv0, geo.msg)) |
83 | sys.exit(1) | |
84 | ||
85 | for key, value in options: | |
86 | if key in ['-h', '--help']: | |
87 | usage() | |
88 | elif key in ['-V', '--version']: | |
8ea171ab | 89 | print("ovs-pcap (Open vSwitch) @VERSION@") |
7aa697dd BP |
90 | else: |
91 | sys.exit(0) | |
92 | ||
93 | if len(args) != 1: | |
94 | sys.stderr.write("%s: exactly 1 non-option argument required " | |
95 | "(use --help for help)\n" % argv0) | |
96 | sys.exit(1) | |
97 | ||
98 | reader = PcapReader(args[0]) | |
99 | while True: | |
100 | packet = reader.read() | |
101 | if packet is None: | |
102 | break | |
103 | ||
7d8eadce | 104 | print(binascii.hexlify(packet).decode().strip()) |
7aa697dd | 105 | |
f3068bff | 106 | except PcapException as e: |
7aa697dd BP |
107 | sys.stderr.write("%s: %s\n" % (argv0, e)) |
108 | sys.exit(1) | |
109 | ||
110 | # Local variables: | |
111 | # mode: python | |
112 | # End: |