gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:36:31 27 4
gpt4 key购买 nike

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ç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
pipe grep

最佳答案

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 添加更多引号,并且您的命令的参数是错误的。

除非您需要分别传递每个参数,但您这样做的尝试也失败了,因为您在第一个和最后一个参数中留下了引号。这并不能解决问题:

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

应该是

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

关于python - CalledProcessError : Command '(' grep', 'route' )' 返回非零退出状态 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48398211/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com