CalledProcessError:命令'('grep','route')'返回非零退出状态1 [英] CalledProcessError: Command '('grep', 'route')' returned non-zero exit status 1

查看:61
本文介绍了CalledProcessError:命令'('grep','route')'返回非零退出状态1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python3
OSX 10.13.2

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

I am trying to make FACEBOOK_WHITELIST in order to prevent malicious attack from the internet when listening to webhook
I am in the process of getting the IP addresses. The command is very simple just whois, pipe, and grep.

问题:

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.

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

Then I tried smaller function call. Neither of them are work. First one is single string in single quote

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

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

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

Second one is split string into multiple double quoted string

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

Update:
@Jean-François Fabre

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
管道grep

推荐答案

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

您引用最后一个参数的尝试与 Popen 的引用是多余的.我无法测试,但是您必须删除输入的多余引号:

your attempt to quote the last aegument is redundant with the quoting done by Popen. I can't test, but you have to remove the extra quotes you've thrown in:

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

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

Popen detects that there are spaces in the argument and quotes it automatically if needed. Adding more quotes makes Popen add even more quotes and the arguments of your commands are wrong.

除非您需要分别传递每个参数,否则尝试这样做也将失败,因为您将引号留在了第一个&中.最后一个论点.没用:

unless you need to pass each argument separately but your attempt in doing this also fails because you left the quotes in the first & last argument. That doesn't cut it:

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

应该是

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

这篇关于CalledProcessError:命令'('grep','route')'返回非零退出状态1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆