Skip to content
Advertisement

CalledProcessError: Command ‘(‘grep’, ‘route’)’ returned non-zero exit status 1

Python3
OSX 10.13.2

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.

Problem:

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

Trackback:

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

Where am I wrong?

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

References:
CallProcessError
pipe grep

Advertisement

Answer

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

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

should be

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