0

I use Django 1.6 and Python 2.7

It is my tcpdump command.

sudo /usr/sbin/tcpdump -n -X port 3331

And I kick the command from python because I'd like to use it in Django.

import subprocess as sub

def tcpdump(request, port):    
    result = 'nothing'

    count = 0
    proc = sub.Popen(['sudo', 'tcpdump', '-n', '-X', 'port', str(port)], stdout=sub.PIPE)    

    try:
        for row in proc.stdout:
            print row.rstrip()   # process here
            result = str(row.rstrip())
            count += 1
            if count > 10:
                break
    except:
        print 'tcpdump error'
        proc.terminate()

    return HttpResponse(result)

I wrote in the views.py.

urls.py

url(r'^tcpdump(?P<port>\d+)/$', tcpdump),

I will just access to http://some.url/tcpdump3331

This row.rstrip() get multiple lines string, and I'd like to do timeout.

Could you tell me better practice?

shinriyo
  • 344
  • 3
  • 17
  • 1
    Is the `tcpdump` command run from the Django app ? Or is it from a different process ? Why can't you simply use `tcpdump` from the shell/bash script/cron job/at process ? What should trigger the port capture ? How long should run the capture ? As a final note: _"I'd like to use it in Django"_ **having your web server running with enough privileges to either execute `sudo` or to perform a port dump seems a major security threat to me**. But YMMV. – Sylvain Leroux Mar 28 '15 at 08:53
  • What exactly are you trying to do? – syntonym Mar 28 '15 at 09:08
  • Thank you for replying. I'd like to capture some data from the port I selected. – shinriyo Mar 28 '15 at 10:17

1 Answers1

0

You do not need to run tcpdump from your app. The point is that it sniffs the network traffic, and logs network packets regardless of the applications generating the traffic. You do not even have to run it on the same computer, where your server is running, just run in on the same subnet.

You can also use GUI tools like wireshark which is easier to configure.

Balint Domokos
  • 1,021
  • 8
  • 12
  • Thank you for replying. I am sending sound data from client via port, and I'd like to check the data by Django. – shinriyo Mar 28 '15 at 11:07