CalledProcessError:Command'('grep','route')'返回非零退出状态1

时间:2018-01-23 09:27:56

标签: python linux bash

Python3
OSX 10.13.2

我正在尝试制作FACEBOOK_WHITELIST,以便在收听webhook时防止来自互联网的恶意攻击 我正在获取IP地址。该命令非常简单,只有whoispipegrep

问题:

def test():
    import subprocess
    ps = subprocess.Popen(
        ["whois", "-h", "whois.radb.net", "--", "'-i origin AS32934'"],
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT
    )
    output = subprocess.check_output(('grep', 'route'), stdin=ps.stdout)
    ps.wait()

引用:

---------------------------------------------------------------------------
CalledProcessError                        Traceback (most recent call last)
<ipython-input-28-8d5c434d09bd> in <module>()
      5     stderr=subprocess.STDOUT
      6 )
----> 7 output = subprocess.check_output(('grep', 'route'), stdin=ps.stdout)
      8 ps.wait()

~/.pyenv/versions/3.6.4/lib/python3.6/subprocess.py in check_output(timeout, *popenargs, **kwargs)
    334
    335     return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
--> 336                **kwargs).stdout
    337
    338

~/.pyenv/versions/3.6.4/lib/python3.6/subprocess.py in run(input, timeout, check, *popenargs, **kwargs)
    416         if check and retcode:
    417             raise CalledProcessError(retcode, process.args,
--> 418                                      output=stdout, stderr=stderr)
    419     return CompletedProcess(process.args, retcode, stdout, stderr)
    420

CalledProcessError: Command '('grep', 'route')' returned non-zero exit status 1.

然后我尝试了较小的函数调用。它们都不起作用。 第一个是单引号中的单个字符串

In [46]: ps = subprocess.Popen(["whois", "-h", "whois.radb.net", "--", "'-i origin AS32934'"])
    ...:
    ...:

In [47]: %  No entries found for the selected source(s).

第二个是将字符串拆分为多个双引号字符串

In [48]: ps = subprocess.Popen(["whois", "-h", "whois.radb.net", "--", "'-i", "origin", "AS32934'"])

In [49]: %  No entries found for the selected source(s).
%  No entries found for the selected source(s).
%  No entries found for the selected source(s).

我哪里错了?

更新
@ Jean-FrançoisFabre

In [49]: ps = subprocess.Popen(["whois", "-h", "whois.radb.net", "--", "-i", "origin", "AS32934"])

In [50]: %% Attribute name after "-i" is invalid or unsupported.


%  No entries found for the selected source(s).
aut-num:    AS32934
as-name:    Facebook
descr:      Facebook
member-of:  AS-FACEBOOK
import:     from AS-ANY   accept ANY AND NOT {0.0.0.0/0}
export:     to AS-ANY   announce AS-FACEBOOK AND NOT {0.0.0.0/0}
admin-c:    FBNetEng
tech-c:     FBNetEng
notify:     noc@fb.com
mnt-by:     MAINT-AS32934
changed:    vvasilev@fb.com 20170627  #21:09:05Z
source:     RADB

参考文献:
CallProcessError
pipe grep

1 个答案:

答案 0 :(得分:1)

ps = subprocess.Popen(["whois", "-h", "whois.radb.net", "--", "'-i origin AS32934'"])# Wrong

你引用最后一个论点的尝试是多余的Popen引用。我无法测试,但你必须删除你引用的额外引号:

ps = subprocess.Popen(["whois", "-h", "whois.radb.net", "--", "-i origin AS32934"])

Popen检测到参数中有空格并在需要时自动引用它。添加更多引号会使Popen添加更多引号,并且命令的参数是错误的。

除非您需要单独传递每个参数,但您尝试这样做也会失败,因为您将引号保留在第一个&amp;最后的论点。这不会削减它:

ps = subprocess.Popen(["whois", "-h", "whois.radb.net", "--", "'-i", "origin", "AS32934'"]# Wrong

应该是

ps = subprocess.Popen(["whois", "-h", "whois.radb.net", "--", "-i", "origin", "AS32934"]