从Docker容器中的Python脚本运行自定义Shell命令

时间:2018-07-27 19:54:38

标签: python bash docker

我正在尝试从docker容器内的python脚本运行自定义shell命令,并捕获输出以供以后使用。我的代码是:

def live_who_is(domain):
        live_data = subprocess.check_output('whois {}'.format(domain), shell=True, stderr=subprocess.STDOUT)
        print(live_data)

运行该错误消息:

Traceback (most recent call last):
  File "who_is_compare_script.py", line 158, in <module>
    live_who_is('google.com')
  File "who_is_compare_script.py", line 149, in live_who_is
    live_data = subprocess.check_output('whois {}'.format(domain), shell=True, stderr=subprocess.STDOUT)
  File "/usr/local/lib/python2.7/subprocess.py", line 223, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command 'whois google.com' returned non-zero exit status 1

如果将命令替换为“ ls -a”或什至“ ls {}”。format(“-a”),则它有效。

1 个答案:

答案 0 :(得分:0)

我将行更改为subprocess.call('whois {}'。format(domain),shell = True)并且有效