0

I'm writing a python script that executes tcpdump through subprocess.open

Now, I want this script to not output anything to the screen(stdout), but just run silently. I've tried doing this:

ps_dump = subprocess.Popen(['tcpdump', '-U', '-w', pcapfile, 'port 53'], stdout=subprocess.PIPE)

But it still outputs these:


device eth0 entered promiscuous mode tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes

0 packets captured

0 packets received by filter

0 packets dropped by kernel

device eth0 left promiscuous mode


I want to completely suppress these. If i run the script it shouldn't print anything. I read that you can suppress in linux using >/dev/null but how do I use it in the Popen command above?

Any suggestions or answer as to how to get this to work?

vin_
  • 55
  • 3
  • 5
  • I think @λuser has it right - tcpdump is writing to stderr and you aren't capturing that output. For more information see: http://stackoverflow.com/questions/10683184/piping-popen-stderr-and-stdout – Loring Sep 29 '16 at 19:50

1 Answers1

0

You can pass a file descriptor to the stdout= argument, so simply:

with open(os.devnull, 'w') as nullfd:
    proc = Popen(..., stdout=nullfd)
λuser
  • 893
  • 8
  • 14
  • Hmm doesn't seem to work... I tried it before asking this question – vin_ Sep 29 '16 at 14:48
  • 1
    @vinwin It does work on my computer with a command like `ls`. Are you sure `tcpdump` doesn't output on stderr? – λuser Sep 29 '16 at 15:06