簡體   English   中英

子進程 python 多個命令

[英]subprocess python multiple commands

想打開 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)

到目前為止,這就是我所擁有的,使用ssh.communicate[<command>]給出了正確的 output 但在第一個命令之后關閉了子進程,有什么想法嗎?

為我工作

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