Python 子流程变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4348524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Subprocess Variables
提问by John Riselvato
1 import subprocess
2 raw = raw_input("Filename:").lower()
3 ip = raw_input("Host:").lower()
4 cmd = subprocess.call("tcpdump -c5 -vvv -w" + " raw " + " ip ",shell=True)
So this is my script. I everything works besides one key objective, using the raw input. It allows me to input anything i want, but when it goes to saving the file or using an ip/host doe doesn't actually do anything. Sure it gives me the packets, but from the localhost not the host i type in.
所以这是我的脚本。我使用原始输入,除了一个关键目标之外,一切都在工作。它允许我输入任何我想要的东西,但是当它去保存文件或使用 ip/host doe 时实际上并没有做任何事情。当然它给了我数据包,但来自本地主机而不是我输入的主机。
how i know this isn't working is cause my first raw input is the filename, so i put in test, when i look in the folder were my script is, it produces a file called "raw" meaning, its not actually taking my input only using whats inside my "X"...
我怎么知道这不起作用是因为我的第一个原始输入是文件名,所以我进行了测试,当我查看我的脚本所在的文件夹时,它会生成一个名为“原始”的文件,意思是它实际上并没有占用我的仅使用我的“X”中的内容输入...
So i make a few chances to come to this:
所以我有一些机会来解决这个问题:
1 import subprocess
2 raw = raw_input("Filename:").lower()
3 ip = raw_input("Host:").lower()
4 cmd = subprocess.call("tcpdump -c5 -vvv -w" + raw + "host" + ip,shell=True)
Which is great because it actually calls for the -w but it saves it now as rawhostip instead of "raw"s input. for reference this is what the command looks like in the terminal:
这很棒,因为它实际上需要 -w 但它现在将其保存为 rawhostip 而不是“raw”的输入。供参考,这是命令在终端中的样子:
tcpdump -c5 -vvv -w savename host wiki2
the only two variabls are savename and wiki2 the rest are needed for the command to work.
唯一的两个变量是 savename 和 wiki2,其余的都是命令工作所必需的。
with this script i get this error:
使用此脚本,我收到此错误:
import subprocess
raw = raw_input("Filename:").lower()
ip = raw_input("Host:").lower()
cmd = subprocess.call("tcpdump -c5 -vvv -w" + raw, "host" + ip,shell=True)
Error:
错误:
Traceback (most recent call last):
File "te.py", line 4, in <module>
cmd = subprocess.call("tcpdump -c5 -vvv -w" + raw, "host" + ip,shell=True)
File "/usr/lib/python2.6/subprocess.py", line 480, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.6/subprocess.py", line 583, in __init__
raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer
I am at a lost. Any help will be great, yes I know look at subprocess's doc's on site:X, I have I need a human to teach me, I don't understand what I am reading.
我不知所措。任何帮助都会很棒,是的,我知道在现场查看子流程的文档:X,我需要一个人来教我,我不明白我在读什么。
My question is how do I work with these variables.
我的问题是如何处理这些变量。
采纳答案by user225312
Don't use shell=True. That should be False.
不要使用shell=True. 那应该是False。
You are making subtle mistakes with the input. Specifically, if you have two strings:
您在输入时犯了细微的错误。具体来说,如果您有两个字符串:
>>> s1 = 'Hello'
>>> s2 = 'Hi'
>>> s1 + s2
'HelloHi'
Notice, there is no space between Helloand Hi. So don't do this. (Your line 4)
请注意,Hello和之间没有空格Hi。所以不要这样做。(你的第 4 行)
You should do (the good way):
你应该这样做(好方法):
>>> raw = raw_input('Filename: ')
Filename: test
>>> ip = raw_input('Host: ')
Host: 192.168.1.1
>>> command = 'tcpdump -c5 -vvv -w {0} {1}'.format(raw, ip) # the command goes here
>>> subprocess.call(command.split(), shell=False) # call subprocess and pass the command as a list using split
Now it should work.
现在它应该可以工作了。
回答by Philipp
You should not use the string form ob the subprocessfunctions. Try:
您不应在subprocess函数中使用字符串形式。尝试:
subprocess.check_call(["tcpdump", "-c5", "-vvv", "-w", raw, "host", ip])

