开发者

os.popen() variables

So i have this application thats rather complicated... To keep it short this is the main things you should know:

file_name = raw_input("Name your file:").lower()

os.popen(sudo tcpdump -c5 -vvv -w "file_name" host wiki or host wiki2)

but this doesn't seem to work. Can i get a fix? Thanks, Pastelinux

edit1: Alright this is what i have now, but its not working, any pointers开发者_JS百科 on how to fix?

import subprocess, os, sys

filename = raw_input('File name:').lower

pipe = os.popen("sudo tcpdump -c5 -w", 'filename')

pipe = popen("sudo tcpdump -c5 -w", shell=True, stdout=PIPE).stdout


Don't use `os.popen', you should use the subprocess module.

I am assuming this (a string):

sudo tcpdump -c5 -vvv -w "file_name" host wiki or host wiki2

is your command.

Since we just need to call tcpdump and not get any output from it, we use subprocess.call

>>> subprocess.call(your_command.split())

Of course why it is not working depends on what the error is, but this is how you should go about it. If you can update the answer with your error, then it can be clear.


Your problem is you don't pass a string as your command. Fixed call to popen():

os.popen('sudo tcpdump -c5 -vvv -w {0} host wiki or host wiki2'.format(file_name))


If you are running newer versions of Python, then use subprocess module.

See the replacement details of os.popen at :

  • http://docs.python.org/library/subprocess.html#replacing-os-popen-os-popen2-os-popen3

It says: On Unix, os.popen2, os.popen3 and os.popen4 also accept a sequence as the command to execute, in which case arguments will be passed directly to the program without shell intervention.

pipe = os.popen("cmd", 'r', bufsize)
==>
pipe = Popen("cmd", shell=True, bufsize=bufsize, stdout=PIPE).stdout
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜