0

i have implemented a python program using Mrjob to capture network packets and then plotting the graph.

from mrjob.job import MRJob
import socket
import struct
import sys
import time
import matplotlib.pyplot as plt

import pyshark

class NetworkTrafficJob(MRJob):
    def mapper(self, _, line):
        # Capture network packets
        interface = "wlp2s0"
        duration = 10
        capture = pyshark.LiveCapture(interface=interface)
        start_time = time.time()

        for packet in capture.sniff_continuously():
            if time.time() - start_time >= duration:
                break

            packet_len = len(packet)
            current_time = time.time()
            yield (current_time, packet_len)

    def reducer(self, key, values):
        # Plot the graph of packet length versus time
        x = []
        y = []
    
        for value in values:
            x.append(value[0])
            y.append(value[1])
        
        plt.plot(x, y)
        plt.xlabel("Time (s)")
        plt.ylabel("Packet Length (bytes)")
        plt.title("Network Traffic")
        plt.savefig('~/Desktop/my_plot.png')
        plt.show()

if __name__ == "__main__":
    NetworkTrafficJob.run()

i tried running it using this command python3 ProcessNetworkStream.py test.txt where test.txt is just an empty file becuase mrjob asks for an input file When i run it the plot does not show instead this is the ouput:

No configs found; falling back on auto-configuration
No configs specified for inline runner
Creating temp directory /tmp/PocessesNetworkStream.hani.20230212.104033.753802
Running step 1 of 1...
job output is in /tmp/PocessesNetworkStream.hani.20230212.104033.753802/output
Streaming final output from /tmp/PocessesNetworkStream.hani.20230212.104033.753802/output...
Removing temp directory /tmp/PocessesNetworkStream.hani.20230212.104033.753802...

What should i do???

mkrieger1
  • 19,194
  • 5
  • 54
  • 65

0 Answers0