python子进程popen运行多个命令

2024-06-23 20:10:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在Python中调用多个命令。我在窗户上安装了油灰。在

  1. ssh到Linux机器

    ssh_command_string: plink -i yourppk.ppk ec2-user@instanceIp
    
  2. 从该计算机调用一个命令,该命令将生成一个文件

    export_project_command_string: ./AppManage -export -out /opt/notme-Facebook.xml -app "Silver Fabric/notme-FacebookPostsExtraction0118" -user username-pw password -domain ion
    
  3. 下载此文件

    download_command_string: pscp -scp -i yourppk.ppk ec2-user@instanceIp:/opt/notme-Facebook.xml d:\
    

当我逐个测试这些命令时,它们是可以的,但是我不知道如何在Python中同时调用它们。在

我尝试过以下代码,但没有成功:

LINE_BUFFERED = 1
    #NOTE: the first argument is a list
    p = Popen(['cat'], shell = True, bufsize=LINE_BUFFERED, stdin=PIPE, stdout = PIPE, stderr = PIPE)
    for cmd in [ssh_command_string, cd_command_string, export_project_command_string, download_command_string]:
        time.sleep(1) # a delay to see that the commands appear one by one
        p.stdin.write(cmd)
        p.stdin.flush()
    # even without .flush() it works as expected on my machine
    p.stdin.close()

Tags: 文件命令projectstringstdinexportec2ssh

热门问题