简体   繁体   English

子进程 python 多个命令

[英]subprocess python multiple commands

would like to open an ssh session, run commands and get the output real-time as the process runs (this base will involve running additional commands on the remote server)想打开 ssh session,运行命令并在进程运行时实时获取 output(此基础将涉及在远程服务器上运行其他命令)

from subprocess import Popen, PIPE

with Popen(['ssh <server-domain-name>',
            ],shell=True,
           stdin=PIPE, stdout=PIPE, stderr=PIPE,
           universal_newlines=True) as ssh:
    output1 = ssh.stdin.write('ls -l')
    output2 = ssh.stdin.write('mkdir test')
    status = ssh.poll()

print(output1)
print(output2)

so far this is what I have, using ssh.communicate[<command>] gives the right output but closes the subproceess after the first command, any thoughts?到目前为止,这就是我所拥有的,使用ssh.communicate[<command>]给出了正确的 output 但在第一个命令之后关闭了子进程,有什么想法吗?

worked for me为我工作

from fabric2 import Connection
with Connection('<host>') as c:
    print(CGREEN +'connected succsfully!' + CEND)

    #gather user info
    user = io.StringIO
    user = c.run("whoami", hide=True)
    print(f'user found:{user.stdout} ')

    #fetching files
    c.run(<command>, pty=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM