子流程变量

发布于 2024-10-06 02:22:48 字数 1603 浏览 3 评论 0原文

  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)

这就是我的脚本。除了使用原始输入的一个关键目标之外,一切都正常。 它允许我输入任何我想要的东西,但是当它保存文件或使用 ip/主机时,doe 实际上并没有做任何事情。 当然它给了我数据包,但是来自本地主机而不是我输入的主机。

我如何知道这不起作用是因为我的第一个原始输入是文件名,所以我进行了测试,当我查看文件夹时是我的脚本是,它生成一个名为“raw”的文件,意思是,它实际上并不只使用我的“X”中的内容来获取我的输入......

所以我有几次机会来实现这一点:

  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)

这很棒,因为它实际上需要-w 但现在将其保存为 rawhostip 而不是“raw”输入。 作为参考,该命令在终端中的外观如下:

tcpdump -c5 -vvv -w savename host wiki2

唯一的两个变量是 savename 和 wiki2,其余变量是命令正常工作所必需的。

使用此脚本我收到此错误:

import subprocess
raw = raw_input("Filename:").lower()
ip = raw_input("Host:").lower()
cmd = subprocess.call("tcpdump -c5 -vvv -w" + raw, "host" + ip,shell=True)

错误:

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

我迷路了。任何帮助都会很棒,是的,我知道在现场查看子流程的文档:X,我需要一个人来教我,我不明白我在读什么。

我的问题是如何处理这些变量。

  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.

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"...

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:

tcpdump -c5 -vvv -w savename host wiki2

the only two variabls are savename and wiki2 the rest are needed for the command to work.

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.

My question is how do I work with these variables.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云归处 2024-10-13 02:22:48

不要使用shell=True。这应该是False

您在输入时犯了一些微妙的错误。具体来说,如果您有两个字符串:

>>> s1 = 'Hello'
>>> s2 = 'Hi'
>>> s1 + s2
'HelloHi'

请注意,HelloHi 之间没有空格。所以不要这样做。 (你的第 4 行)

你应该做(好方法):

>>> 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

现在应该可以了。

Don't use shell=True. That should be 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 Hello and Hi. So don't do this. (Your line 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.

温折酒 2024-10-13 02:22:48

您不应在 subprocess 函数中使用字符串形式。尝试:

subprocess.check_call(["tcpdump", "-c5", "-vvv", "-w", raw, "host", ip])

You should not use the string form ob the subprocess functions. Try:

subprocess.check_call(["tcpdump", "-c5", "-vvv", "-w", raw, "host", ip])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文