1

I am trying to send GET requests but before starting the requests, I'd like to capture the traffic. Capturing traffic can be done with the command:

dumpcap -i eth0 -f "udp port 53" -w dns.cap

in the background. While I capture packets, I need to make some request by sending some URLs. For now, with the code below, it seems my capturing code does not work, I can not even see a dns.cap file in my folder.

What's the problem?

import requests
import os
import subprocess
import urllib
print "start capturing packets...\n"
#os.system("dumpcap -i eth0 -f \"udp port 53\" -w dns.cap"
os.spawnl(os.P_NOWAIT,'dumpcap -i eth0 -f \"udp port 53\" -w dns.cap')
print urllib.urlopen('http://www.google.com').read()
#resp = requests.get('http://httpbin.org')
#resp=requests.get('http://httpbin.org')
print "ok"
2
  • Do you run the script as root? Try to specify full path to 'dns.cap' file. Commented Dec 23, 2012 at 13:38
  • yes as a root, no luck after specifying the exact path. Commented Dec 23, 2012 at 13:40

1 Answer 1

1

os.spanwl is considered old and should be replaced with subprocess.Popen. Replace os.spanwnl call with this:

subprocess.Popen(['/usr/bin/dumpcap', '-i', 'eth0', '-f', 'udp port 53', 
                  '-w', '/tmp/dns.cap'])

It's better to add some pause (sleep) after starting the dumpcap to ensure that capturing is established when you make requests.

Sign up to request clarification or add additional context in comments.

2 Comments

i tested, it works thanks, what about following code is there any difference than your post. subprocess.Popen(['/usr/bin/dumpcap', '-i eth0', '-f', 'udp port 53', '-w', '/tmp/dns.cap']), In addition my other code while processing packets really long is there easy way to adapt subprocess.Popen while using long system commands with arguments?
'-i eth0' will not work, you need to specify each argument in separate array item. There is no problem with long system commands with arguments. You can write '-i', 'eth0' in 2 array items, and you can write '--long-command=eth0' in a single array item.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.