在Python脚本中运行一系列外部命令

时间:2022-07-07 16:22:41

I'm trying to run external commands (note the plural) from a Python script. I've been reading about the subprocess module and use it. It works for me when I have a single or independent commands to run, whether I'm interested in the stdout or not.

我正在尝试从Python脚本运行外部命令(注意复数)。我一直在阅读有关子进程模块并使用它的信息。当我有单个或独立的命令运行时,它对我有用,无论我是否对stdout感兴趣。

What I want to do is a bit different: I want something persistent. Basically the first command I run is to log in an application, then I can run some other commands which only work if I'm logged in. For some of these commands, I need the stdout.

我想做的是有点不同:我想要持久的东西。基本上我运行的第一个命令是登录一个应用程序,然后我可以运行一些其他命令,这些命令只有在我登录时才有效。对于其中一些命令,我​​需要stdout。

So when I use subprocess to login, it does it, but then the process is killed and next time I run a command with subprocess, I'm not logged in anymore... I just need to run a series of commands, like I would do it in a terminal.

因此,当我使用子进程登录时,它会这样做,但是然后进程被终止,下次我用子进程运行命令时,我不再登录了...我只需要运行一系列命令,比如我会在终端做到这一点。

Any idea how to do that?

知道怎么做吗?

1 个解决方案

#1


0  

You can pass in an arbitrarily complex series of commands with shell=True though I would generally advise against doing that, if not least because you are making your Python script platform dependent.

您可以使用shell = True传递任意复杂的一系列命令,但我通常建议不要这样做,如果不是因为您正在使您的Python脚本平台依赖。

result = subprocess.check_output('''
    servers=0
    for server in one two three four; do
        output=$(printf 'echo moo\necho bar\necho baz\n' | ssh "$server")
        case $output in *"hello"*) echo "$output";; esac
        echo "$output" | grep -q 'ALERT' && echo "$server: Intrusion detected"
        servers=$((servers++))
    done
    echo "$servers hosts checked"
    ''', shell=True)

One of the problems with shell script (or I guess Powershell or cmd batch script if you are in that highly unfortunate predicament) is that doing what you are vaguely describing is often hard to do with a bunch of unconnected processes. E.g. curl has a crude way to maintain a session between separate invocations by keeping a "cookie jar" which allows one curl to pass on login credentials etc to an otherwise independent curl call later on, but there is no good, elegant, general mechanism for this. If at all possible, doing these things from within Python would probably make your script more robust as well as simpler and more coherent.

shell脚本的一个问题(或者,如果你处于那种非常不幸的困境,我想是Powershell或cmd批处理脚本)就是做一些模糊描述的东西通常很难处理一堆未连接的进程。例如。 curl有一种粗略的方法来保持单独调用之间的会话,通过保留一个“cookie jar”允许一个curl将登录凭据等传递给以后独立的curl调用,但是没有好的,优雅的,通用的机制。如果可能的话,在Python中执行这些操作可能会使您的脚本更加健壮,更简单,更连贯。

#1


0  

You can pass in an arbitrarily complex series of commands with shell=True though I would generally advise against doing that, if not least because you are making your Python script platform dependent.

您可以使用shell = True传递任意复杂的一系列命令,但我通常建议不要这样做,如果不是因为您正在使您的Python脚本平台依赖。

result = subprocess.check_output('''
    servers=0
    for server in one two three four; do
        output=$(printf 'echo moo\necho bar\necho baz\n' | ssh "$server")
        case $output in *"hello"*) echo "$output";; esac
        echo "$output" | grep -q 'ALERT' && echo "$server: Intrusion detected"
        servers=$((servers++))
    done
    echo "$servers hosts checked"
    ''', shell=True)

One of the problems with shell script (or I guess Powershell or cmd batch script if you are in that highly unfortunate predicament) is that doing what you are vaguely describing is often hard to do with a bunch of unconnected processes. E.g. curl has a crude way to maintain a session between separate invocations by keeping a "cookie jar" which allows one curl to pass on login credentials etc to an otherwise independent curl call later on, but there is no good, elegant, general mechanism for this. If at all possible, doing these things from within Python would probably make your script more robust as well as simpler and more coherent.

shell脚本的一个问题(或者,如果你处于那种非常不幸的困境,我想是Powershell或cmd批处理脚本)就是做一些模糊描述的东西通常很难处理一堆未连接的进程。例如。 curl有一种粗略的方法来保持单独调用之间的会话,通过保留一个“cookie jar”允许一个curl将登录凭据等传递给以后独立的curl调用,但是没有好的,优雅的,通用的机制。如果可能的话,在Python中执行这些操作可能会使您的脚本更加健壮,更简单,更连贯。