您可能已经解决了这个问题,但我认为无论如何我都会提供答案,因为我对pyshark及其功能感兴趣。
希望这些答案对您有用。
示例一
# Network interface used by TShark for live capture
network_interface = 'en0'
capture = pyshark.LiveCapture(interface=network_interface)
capture.sniff(timeout=50)
for raw_packet in capture.sniff_continuously():
try:
# Only looks at TCP packets
if hasattr(raw_packet, 'tcp'):
source_address = raw_packet.ip.src
source_port = raw_packet[raw_packet.transport_layer].srcport
destination_address = raw_packet.ip.dst
destination_port = raw_packet[raw_packet.transport_layer].dstport
ack_rtt = raw_packet[raw_packet.transport_layer].analysis_ack_rtt
# analysis_lost_segment can produce multiple messages:
#
# (1) 'tcp previous segment not captured.
# This message is created when TShark didn't see a packet that should have been in the trace.
# This warning was previously called "tcp previous segment lost"
#
# (2) 'Previous segment not captured (common at capture start)'
# This means that on the receiver side you capture an outgoing ACK packet
# for a sequence number where you haven't seen the respective segment.
# This is common, as it might be possible that a segment arrived,
# you started the capture and afterwards your TCP stack replied
# with an ACK. So there was no way to see the incoming packet.
#
lost_segment = raw_packet[raw_packet.transport_layer].analysis_lost_segment
print(f'Source Address: {source_address}\n'
f'Source Port: {source_port}\n'
f'Destination address: {destination_address}\n'
f'Destination port:{destination_port}\n'
f'RTT to ACK was: {ack_rtt} seconds\n'
f'{lost_segment}\n')
# PRINT OUTPUT
Source Address: 192.168.86.35
Source Port: 64490
Destination address: 31.13.66.174
Destination port:443
RTT to ACK was: 0.000162000 seconds
Previous segment(s) not captured (common at capture start)
except AttributeError as e:
pass
示例二
# Network interface used by TShark for live capture
network_interface = 'en0'
capture = pyshark.LiveCapture(interface='en0', display_filter='tcp.analysis.ack_rtt or tcp.analysis.lost_segment')
capture.sniff(timeout=50)
for raw_packet in capture.sniff_continuously():
try:
source_address = raw_packet.ip.src
source_port = raw_packet[raw_packet.transport_layer].srcport
destination_address = raw_packet.ip.dst
destination_port = raw_packet[raw_packet.transport_layer].dstport
ack_rtt = raw_packet[raw_packet.transport_layer].analysis_ack_rtt
# analysis_lost_segment can produce multiple messages:
#
# (1) 'tcp previous segment not captured.
# This message is created when TShark didn't see a packet that should have been in the trace.
# This warning was previously called "tcp previous segment lost"
#
# (2) 'Previous segment not captured (common at capture start)'
# This means that on the receiver side you capture an outgoing ACK packet
# for a sequence number where you haven't seen the respective segment.
# This is common, as it might be possible that a segment arrived,
# you started the capture and afterwards your TCP stack replied
# with an ACK. So there was no way to see the incoming packet.
#
lost_segment = raw_packet[raw_packet.transport_layer].analysis_lost_segment
print(f'Source Address: {source_address}\n'
f'Source Port: {source_port}\n'
f'Destination address: {destination_address}\n'
f'Destination port: {destination_port}\n'
f'RTT to ACK was: {ack_rtt} seconds\n'
f'{lost_segment}\n')
# PRINT OUTPUT
Source Address: 192.168.86.35
Source Port: 64490
Destination address: 31.13.66.174
Destination port: 443
RTT to ACK was: 0.000162000 seconds
Previous segment(s) not captured (common at capture start)
except AttributeError as e:
pass