【问题标题】:Why the command isn't being executed in SSH? Python为什么命令没有在 SSH 中执行? Python
【发布时间】:2013-10-26 04:18:45
【问题描述】:

我截断了以下代码,以允许在两台 Windows 机器上执行。我安装了copssh 以在 Windows 机器中作为 ssh 服务器工作,我正试图让它工作......

    cmd = "python --version"
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.WarningPolicy())
    client.connect("myhost",username="admin", password="adminpass")

    print "restarting machine ... "
    stdin, stdout, stderr = client.exec_command(cmd)
    print stdout.readlines()

当执行以下代码时,我得到:

C:\wcat>python main.py
C:\Python27\lib\site-packages\paramiko-1.11.0-py2.7.egg\paramiko\client.py:95: U
serWarning: Unknown ssh-rsa host key for 192.168.2.4: 7bf6ba243a3daaa994e0b42067
9cfb30
  (key.get_name(), hostname, hexlify(key.get_fingerprint())))
restarting machine ...
[]

什么都不返回...即使是像cd C:\ && dir 这样的简单Windows 命令也会返回[]。我也在从我正在 ssh-ing 到另一台机器的机器上做同样的事情,它可以工作,但解决方法不是......可能的原因是什么?

【问题讨论】:

  • @Kyle Stderr <paramiko.ChannelFile from <paramiko.Channel 1 (open) window=2097152 -> < paramiko.Transport at 0x2a03bd0L (cipher aes128-ctr, 128 bits) (active; 1 open c hannel(s))>>>
  • @philippe:看起来它只是标准错误的repr:如果你从文件中读取(例如,print stderr.read()),你会得到什么吗?
  • @Thanatos ...当我打印 stderr.read() 我什么也没得到...print stdout.channel.recv_exit_status() 返回 127 ...这是什么意思?

标签: python windows ssh


【解决方案1】:

除了设置缺少的主机密钥策略外,请确保加载系统主机密钥。

client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.WarningPolicy())

【讨论】:

    【解决方案2】:

    运行python --version 并将输出流式传输到标准错误。您可以使用

    轻松检查它
    python --version >> out 2> err 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-11
      • 2015-03-29
      • 2012-02-26
      • 1970-01-01
      • 2023-03-23
      • 2020-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多