0

I have installed iftop and it works fine on the command line now when I try and integrate it into Django, It does not give me any output at all but at the same time I am not getting an errors.

views.py

def packets(request, template="linux_path/packets.html"):

context = {}
tcp_data = subprocess.Popen(['sudo', 'iftop','-i', 'eth1'], stdout=subprocess.PIPE)
raw_packets = tcp_data.stdout.read()
groups = []
for raw_packet in raw_packets.split("\n"):

    tokens = re.match("[5:5]", raw_packet)
    if tokens is None:
        continue
    else:
        packet_name, packet_length = tokens.groups()

    group = {
        "packet_name":packet_name, 
        "packet_length":packet_length
        }
    groups.append(groups)
return groups

HTML

{% extends "base.html" %}

{% block title %}
  Packet Log
{% endblock %}

{% block content %}

<table style="width: 100%">
  <thead> 
    <th>Name</th>
  </thead>
  <tbody>

    <tr>
      <td>{{function call here}}</td>
    </tr>

  </tbody>
</table>

{% endblock %}

I have also put in the correct code in the urls.py file

What happens is that the page just hangs on loading and does not move on into my packets.html page

Any idea of what I am doing wrong?

Once I get that working then I will be able to manipulate the data to the format that I want using RE.

I have even entered the sudo password into the command line when that option comes up but still the page hangs.

AKaY
  • 17
  • 6

1 Answers1

1

iftop uses ncurses, which means you won't capture it with STDOUT. Looking at the help, you can provide this option:

-t text output mode
       Use text interface without ncurses and print the output to STDOUT.

So try passing this argument in, it may solve your problem.

There's a problem here though, your web server shouldn't be allowed to sudo. That's a serious security problem. You could consider making a very simple Django app which runs only this command and doesn't handle any other data to reduce the attack surface.

Joe
  • 46,419
  • 33
  • 155
  • 245
  • Cool thanks, The sudo option is just for now so that I can see some output. once i start seeing the output then will change that around and add it into my scripting files. I will have a look at the ncurses now. – AKaY Mar 03 '15 at 12:08
  • This looks relevant: http://stackoverflow.com/questions/8371877/ncurses-and-linux-pipeline-c – Joe Mar 03 '15 at 13:47