【问题标题】:How to send EOF to stdin in paramiko?如何将 EOF 发送到 paramiko 中的标准输入?
【发布时间】:2010-03-31 15:36:00
【问题描述】:

我想通过 ssh 执行一些程序并从文件重定向它的输入。以下代码的行为:

channel.exec_command('cat')
with open('mumu', 'r') as f:
    text = f.read()
    nbytes = 0
    while nbytes < len(text):
        sent = channel.send(text[nbytes:])
        if sent == 0:
            break
        nbytes += sent

应该等同于(假设公钥认证):

 ssh user@host cat < mumu

但是应用程序挂起等待更多输入。我认为这是因为标准输入流永远不会关闭。我该怎么做?

【问题讨论】:

    标签: python stdin eof paramiko


    【解决方案1】:

    在频道上致电shutdown()(或shutdown_write())。

    【讨论】:

    • 那么 stdout 和 stderr 会发生什么?
    【解决方案2】:

    调用方法:channel.shutdown_write()

    【讨论】:

      【解决方案3】:

      由于我没有明确使用频道,所以我不得不做一些不同的事情。对于任何可能会觉得有帮助的人:

      client = paramiko.SSHClient()
      connection = client.connect(hostname)
      stdin, stdout, stderr = connection.exec_command('cat')
      stdin.write('spam')
      # Close the channel, this results in an EOF for `cat`.
      stdin.channel.shutdown_write()
      # stdout/stderr are readable.
      print(stdout.read().decode())
      print(stderr.read().decode())
      

      【讨论】:

        猜你喜欢
        • 2016-07-24
        • 1970-01-01
        • 2022-07-08
        • 2014-11-20
        • 1970-01-01
        • 2019-09-03
        • 1970-01-01
        • 2015-12-20
        • 1970-01-01
        相关资源
        最近更新 更多