Skip to content

Commit 336013e

Browse files
committed
Merge pull request #134 from yozik04/master
Added capture filter.
2 parents bc147f2 + 4238023 commit 336013e

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

src/README.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ Reading from a live interface:
5757
for packet in capture.sniff_continuously(packet_count=5):
5858
print 'Just arrived:', packet
5959

60+
Infinite reading from a live interface with capture filter:
61+
------------------------------
62+
63+
::
64+
65+
def packet_captured(packet):
66+
print 'Just arrived:', packet
67+
68+
capture = pyshark.LiveCapture(interface='eth0', capture_filter='tcp')
69+
capture.apply_on_packets(packet_captured)
6070

6171
Accessing packet data:
6272
----------------------

src/pyshark/capture/capture.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ class Capture(object):
3838

3939
def __init__(self, display_filter=None, only_summaries=False, eventloop=None,
4040
decryption_key=None, encryption_type='wpa-pwd', output_file=None,
41-
decode_as=None, tshark_path=None, override_prefs=None):
41+
decode_as=None, tshark_path=None, override_prefs=None, capture_filter=None):
4242
self._packets = []
4343
self.current_packet = 0
4444
self.display_filter = display_filter
45+
self.capture_filter = capture_filter
4546
self.only_summaries = only_summaries
4647
self.output_file = output_file
4748
self.running_processes = set()
@@ -356,6 +357,8 @@ def get_parameters(self, packet_count=None):
356357
Returns the special tshark parameters to be used according to the configuration of this class.
357358
"""
358359
params = []
360+
if self.capture_filter:
361+
params += ['-f', self.capture_filter]
359362
if self.display_filter:
360363
params += [get_tshark_display_filter_flag(self.tshark_path), self.display_filter]
361364
if packet_count:

src/pyshark/capture/live_capture.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class LiveCapture(Capture):
88
"""
99

1010
def __init__(self, interface=None, bpf_filter=None, display_filter=None, only_summaries=False, decryption_key=None,
11-
encryption_type='wpa-pwk', output_file=None, decode_as=None, tshark_path=None, override_prefs=None):
11+
encryption_type='wpa-pwk', output_file=None, decode_as=None, tshark_path=None, override_prefs=None, capture_filter=None):
1212
"""
1313
Creates a new live capturer on a given interface. Does not start the actual capture itself.
1414
@@ -25,11 +25,12 @@ def __init__(self, interface=None, bpf_filter=None, display_filter=None, only_su
2525
it attempt to decode any port 8888 traffic as HTTP. See tshark documentation for details.
2626
:param tshark_path: Path of the tshark binary
2727
:param override_prefs: A dictionary of tshark preferences to override, {PREFERENCE_NAME: PREFERENCE_VALUE, ...}.
28+
:param capture_filter: Capture (wireshark) filter to use.
2829
"""
2930
super(LiveCapture, self).__init__(display_filter=display_filter, only_summaries=only_summaries,
3031
decryption_key=decryption_key, encryption_type=encryption_type,
3132
output_file=output_file, decode_as=decode_as, tshark_path=tshark_path,
32-
override_prefs=override_prefs)
33+
override_prefs=override_prefs, capture_filter=capture_filter)
3334
self.bpf_filter = bpf_filter
3435

3536
if interface is None:

0 commit comments

Comments
 (0)