Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2015-01-18
2
Medium Priority
?
2,154 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 2000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

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…
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Progress
Suggested Courses

609 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