packet.pktt (3) - Linux Manuals
packet.pktt: Packet trace module
NAME
packet.pktt - Packet trace moduleDESCRIPTION
The Packet trace module is a python module that takes a trace file created by tcpdump and unpacks the contents of each packet. You can decode one packet at a time, or do a search for specific packets. The main difference between these modules and other tools used to decode trace files is that you can use this module to completely automate your tests.
How does it work? It opens the trace file and reads one record at a time keeping track where each record starts. This way, very large trace files can be opened without having to wait for the file to load and avoid loading the whole file into memory.
Packet layers supported:
CLASSES
class Header(baseobj.BaseObj)
Methods defined here:
---------------------
__init__(self, pktt)
class Pktt(baseobj.BaseObj, packet.unpack.Unpack)
Packet trace object
Usage:
from packet.pktt import Pktt
x = Pktt("/traces/tracefile.cap")
# Iterate over all packets found in the trace file
for pkt in x:
print pkt
Methods defined here:
---------------------
__contains__(self, expr)
Implement membership test operator.
Return true if expr matches a packet in the trace file,
false otherwise.
The packet is also stored in the object attribute pkt.
Examples:
# Find the next READ request
if ("NFS.argop == 25" in x):
print x.pkt.nfs
See match() method for more information
__del__(self)
Destructor
Gracefully close the tcpdump trace file if it is opened.
__getitem__(self, index)
Get the packet from the trace file given by the index
or raise IndexError.
The packet is also stored in the object attribute pkt.
Examples:
pkt = x[index]
__init__(self, tfile, live=False, state=True)
Constructor
Initialize object's private data, note that this will not check the
file for existence nor will open the file to verify if it is a valid
tcpdump file. The tcpdump trace file will be opened the first time a
packet is retrieved.
__iter__(self)
Make this object iterable.
clear_xid_list(self)
Clear list of outstanding xids
match(self, expr, maxindex=None, rewind=True, reply=False)
Return the packet that matches the given expression, also the packet
index points to the next packet after the matched packet.
Returns None if packet is not found and the packet index points
to the packet at the beginning of the search.
match_nfs(self, uargs)
Match NFS values on current packet.
In NFSv4, there is a single compound procedure with multiple
operations, matching becomes a little bit tricky in order to make
the matching expression easy to use. The NFS object's name space
gets converted into a flat name space for the sole purpose of
matching. In other words, all operation objects in array are
treated as being part of the NFS object's top level attributes.
Consider the following NFS object:
nfsobj = COMPOUND4res(
status=NFS4_OK,
tag='NFSv4_tag',
array = [
nfs_resop4(
resop=OP_SEQUENCE,
opsequence=SEQUENCE4res(
status=NFS4_OK,
resok=SEQUENCE4resok(
sessionid='sessionid',
sequenceid=29,
slotid=0,
highest_slotid=179,
target_highest_slotid=179,
status_flags=0,
),
),
),
nfs_resop4(
resop=OP_PUTFH,
opputfh = PUTFH4res(
status=NFS4_OK,
),
),
...
]
),
The result for operation PUTFH is the second in the list:
putfh = nfsobj.array[1]
From this putfh object the status operation can be accessed as:
status = putfh.opputfh.status
or simply as (this is how the NFS object works):
status = putfh.status
In this example, the following match expression 'NFS.status == 0'
could match the top level status of the compound (nfsobj.status)
or the putfh status (nfsobj.array[1].status)
The following match expression 'NFS.sequenceid == 25' will also
match this packet as well, even though the actual expression should
be 'nfsobj.array[0].opsequence.resok.sequenceid == 25' or
simply 'nfsobj.array[0].sequenceid == 25'.
This approach makes the match expressions simpler at the expense of
having some ambiguities on where the actual matched occurred. If a
match is desired on a specific operation, a more qualified name can
be given. In the above example, in order to match the status of the
PUTFH operation the match expression 'NFS.opputfh.status == 0' can
be used. On the other hand, consider a compound having multiple
PUTFH results the above match expression will always match the first
occurrence of PUTFH where the status is 0. There is no way to tell
the match engine to match the second or Nth occurrence of an
operation.
next(self)
Get the next packet from the trace file or raise StopIteration.
The packet is also stored in the object attribute pkt.
Examples:
# Iterate over all packets found in the trace file using
# the iterable properties of the object
for pkt in x:
print pkt
# Iterate over all packets found in the trace file using it
# as a method and using the object variable as the packet
# Must use the try statement to catch StopIteration exception
try:
while (x.next()):
print x.pkt
except StopIteration:
pass
# Iterate over all packets found in the trace file using it
# as a method and using the return value as the packet
# Must use the try statement to catch StopIteration exception
while True:
try:
print x.next()
except StopIteration:
break
NOTE:
Supports only single active iteration
rewind(self, index=0)
Rewind the trace file by setting the file pointer to the start of
the given packet index. Returns False if unable to rewind the file,
e.g., when the given index is greater than the maximum number
of packets processed so far.
Static methods defined here:
----------------------------
escape(data)
Escape special characters.
Examples:
# Call as an instance
escaped_data = x.escape(data)
# Call as a class
escaped_data = Pktt.escape(data)
ip_tcp_dst_expr(ipaddr, port)
Return a match expression to find a packet going to ipaddr:port.
Examples:
# Call as an instance
expr = x.ip_tcp_dst_expr('192.168.1.50', 2049)
# Call as a class
expr = Pktt.ip_tcp_dst_expr('192.168.1.50', 2049)
# Returns "IP.dst == '192.168.1.50' and TCP.dst_port == 2049"
# Expression ready for x.match()
pkt = x.match(expr)
ip_tcp_src_expr(ipaddr, port)
Return a match expression to find a packet coming from ipaddr:port.
Examples:
# Call as an instance
expr = x.ip_tcp_src_expr('192.168.1.50', 2049)
# Call as a class
expr = Pktt.ip_tcp_src_expr('192.168.1.50', 2049)
# Returns "IP.src == '192.168.1.50' and TCP.src_port == 2049"
# Expression ready for x.match()
pkt = x.match(expr)
BUGS
No known bugs.
AUTHOR
Jorge Mora (mora [at] netapp.com)
SEE ALSO
baseobj(3),
formatstr(3),
packet.link.ethernet(3),
packet.pkt(3),
packet.record(3),
packet.unpack(3)