【问题标题】:python subprocess popen run multiple commandpython子进程popen运行多个命令
【发布时间】:2013-03-21 09:40:31
【问题描述】:

我想在 Python 中调用多个命令。我在 Windows 上安装了 putty。

  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()

【问题讨论】:

  • 这是与进程通信的错误方式。但我认为你忘记使用 Popen.comunicate 将命令发送到进程......使用你的代码,如果远程 shell 向你发送一些文本,你的缓冲区将被填充,进程将等待释放它,但你不't read p.stdout/p.stderr,所以远程进程永远等待

标签: python amazon popen


【解决方案1】:

查看fabric 在服务器上执行重复/自动任务。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    • 1970-01-01
    • 2012-03-27
    • 2013-11-22
    • 2020-11-26
    • 1970-01-01
    相关资源
    最近更新 更多