Solved

In python how can I redirect the output from tcpdump to a variable

Posted on 2015-01-18
2
1,862 Views
Last Modified: 2015-01-19
Greetings,

I am looking for a way to make this work in python,

f=os.system("tcpdump -l src 172.16.0.3 and dst 172.16.0.33")
print "Dump is ", f

I will then process 'f' downstream of this

This produces an output of tcpdump to the console but nothing to the 'f' string

Thanks,
Ron
0
Comment
Question by:RonWensley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 25

Accepted Solution

by:
clockwatcher earned 500 total points
ID: 40556771
I think you'd be much better served by going with a pcap module (https://code.google.com/p/pypcap/) but you can give this a try:
import StringIO
import sys
import shlex
import subprocess
cmd = "/usr/sbin/tcpdump -l src 172.16.0.3 and dst 172.16.0.33"
args = shlex.split(cmd)
tcpdump = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
print "Press CTRL-C to stop tcpdump"
output = StringIO.StringIO()
running = True
while running:
    try:
        data = tcpdump.stdout.readline()
        if len(data):
            output.write(data)
        else:
            running = False
    except KeyboardInterrupt:
        tcpdump.kill()
        data = tcpdump.stdout.readline()
        if len(data):
            output.write(data)
        running = False

lines = output.getvalue().split("\n")
for i, line in enumerate(lines):
    print "{0} => {1}".format(i, line)

Open in new window

0
 

Author Closing Comment

by:RonWensley
ID: 40558346
Thanks for the quick reply.  This is working out great.  I will have a look at the link you offered and see how that can be used.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Introduction to Processes

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question