Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将Unix命令输出传递给Python函数_Python_Shell_Fabric - Fatal编程技术网

如何将Unix命令输出传递给Python函数

如何将Unix命令输出传递给Python函数,python,shell,fabric,Python,Shell,Fabric,我需要在本地机器上运行一个docker命令,并将此列表发送到远程服务器,检查这些映像是否存在。我需要将远程服务器上不存在的映像列表重新运行到本地服务器。我需要用python来做。我混合使用shell和python编写了一些代码,如下所示 List=$(docker images -q | grep "docker pull" | awk '{print $3}') #this command is mandatory to get exact docker name. fab remote_sy

我需要在本地机器上运行一个docker命令,并将此列表发送到远程服务器,检查这些映像是否存在。我需要将远程服务器上不存在的映像列表重新运行到本地服务器。我需要用python来做。我混合使用shell和python编写了一些代码,如下所示

List=$(docker images -q | grep "docker pull" | awk '{print $3}') #this command is mandatory to get exact docker name.
fab remote_sync_system_spec_docker_to_aws_artifactory:List -u ${USERNAME} -H 1.2.3.4
我正在尝试通过上面的fab将shell命令的输出即List传递给pyhon函数。该函数如下所示

def remote_sync_system_spec_docker_to_aws_artifactory(List):
for line in List:
if( os.popen("docker images -q $line") == none )
List=... #need to prepare list and return back to calling function.
一旦我在远程服务器上获得了列表,我需要将它返回到调用函数,我可以在那里进行一些操作。基本上我可以使用shell,但问题是使用sshpass连接到远程服务器在我的项目中不被接受,所以寻找python脚本

os.popen()
将返回并在内存中对象,您应该做的是

def remote_sync_system_spec_docker_to_aws_artifactory(List):
    for line in List:
        if( os.popen("docker images -q $line").read() == none ):
            List=... #need to prepare list and return back to calling function.

如果只需要从shell命令获取输出,则应避免使用
os.popen()
甚至替换它

对于最新的Python 3.x,请使用
subprocess.run()

在Python2.x中,相应的函数是
子进程。检查输出()


也许你会想用更专注的东西来取代grep的
grep
<代码>'docker pull'in result
将在行中的任何位置查找字符串,但您可能希望将其仅限于特定列,例如。

作为传输列表的简单方法,我建议使用管道而不是变量

docker images -q | awk '/docker pull/ { print $3 }' |
fab remote_sync_system_spec_docker_to_aws_artifactory_stdin -u ${USERNAME} -H 1.2.3.4
其中函数类似于

import sys, subprocess

def remote_sync_system_spec_docker_to_aws_artifactory_stdin (handle=sys.stdin):
    """
    Read Docker image identifiers from file handle; check which
    ones are available here, and filter those out; return the rest.
    """
    missing = ()
    for line in handle:
        repo = line.rstrip('\n')
        if subprocess.run(['docker', 'images', '-q', repo],
                stdout=subprocess.PIPE, universal_newlines=True).stdout == "":
            missing.append(repo)
    return missing

我在此行收到错误fab remote_sync_system_spec_docker_to_aws_artifactory:List-u${USERNAME}-h1.2.3.4,因为意外标记附近出现语法错误`('即使我没有使用括号。我如何将列表传递给python函数。错误消息看起来与此答案中发布的代码无关。我如何使用fab将os.popen的输出传递给python函数?
docker images-q
仅打印十六进制标识符,每行一个。如果您希望输出多个字段,我想您正在查看我不知道Fabric如何改善SSH的情况,它只是掩盖了一个事实,即您正在使用常规SSH。我将用示例解释我的要求。本地服务器中的docker映像返回10个映像,我需要检查远程服务器上有多少映像se 10和我需要将不存在的映像返回到本地服务器。在我的代码列表中,有位于本地计算机上的映像,我正在使用fab命令将其传递到远程服务器。函数def remote_sync_system_spec_docker_to_aws_artifactory(列表):正在检查远程服务器中的图像。我如何将列表传递到此函数并读取其内容。您是否询问如何从Python中访问外壳变量?这与Python中的情况类似,但您的问题根本不清楚您是否需要这样做。我是否应该删除此答案并发布另一个解释?。。。但无论如何,用Python编写整个流程会更有意义;shell只支持字符串,不支持数组或列表(可移植)。我将再次注意到,
docker images-q | awk'/docker pull/
永远无法打印任何有用的内容,并建议您在此处删除
-q
选项。事实上,
docker images-q deadbeef0bad
似乎也不会打印任何内容,因此需要对部分逻辑进行检查嗯。我想你对我的要求感到困惑了。我需要运行一个简单的unix命令,将其输出到一个变量中,并将该变量作为参数传递给一个函数。这应该用python编写。我在这里建议的是不要将其放在变量中。你可以,但它笨重且不美观。从另一个逗号读取输出nd,因为您的标准输入与现有的Unix工具配合得很好,并且允许您以相当优雅和惯用的方式处理问题。最终,我认为您可以用Python完成整个过程,然后在函数之间传递任意复杂的变量不是问题。
import sys, subprocess

def remote_sync_system_spec_docker_to_aws_artifactory_stdin (handle=sys.stdin):
    """
    Read Docker image identifiers from file handle; check which
    ones are available here, and filter those out; return the rest.
    """
    missing = ()
    for line in handle:
        repo = line.rstrip('\n')
        if subprocess.run(['docker', 'images', '-q', repo],
                stdout=subprocess.PIPE, universal_newlines=True).stdout == "":
            missing.append(repo)
    return missing