【问题标题】:ssh host key verification failed if run with subprocess如果使用子进程运行 ssh 主机密钥验证失败
【发布时间】:2016-06-28 13:41:21
【问题描述】:

我有两台运行 Linux a@A 和 b@B 的计算机,一个麦克风连接到 B。 使用 A,我登录到 b@B 以启动一个 python 脚本,它可以实时收听麦克风。

这是我的代码:

#!/usr/bin/env python
# -*-coding:Latin-1 -*

import threading
import time
import pygame
import sys
import subprocess
import os
import signal

global listenProcess
global listenProcess2

listenProcess = subprocess.Popen('arecord -D plughw:1,0 -f dat'.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
listenProcess2 = subprocess.Popen('ssh -C a@A aplay -f dat'.split(), stdin=listenProcess.stdout, stderr=subprocess.PIPE)

如果我启动命令:

arecord -D plughw:1,0 -f dat | ssh -C a@A aplay -f dat

它工作得很好,我可以听到麦克风录制的内容。但如果它来自我的 python 脚本,则会引发以下错误:

主机密钥验证失败

当出现错误时,我已尝试按照建议的方式“清理”known_hosts 文件。 我也尝试过 paramiko,但似乎不是我的解决方案。

我的想法已经用完了。

谢谢

编辑: 运行arecord -D plughw:1,0 -f dat | ssh -v -C bertrand@192.168.1.33 aplay -f dat 显示:

pi@raspberrypi ~/Documents $ arecord -D plughw:1,0 -f dat | ssh -v -C bertrand@192.168.1.33 aplay -f dat
Recording WAVE 'stdin' : Signed 16 bit Little Endian, Rate 48000 Hz, Stereo
OpenSSH_6.7p1 Raspbian-5+deb8u1, OpenSSL 1.0.1k 8 Jan 2015
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 192.168.1.33 [192.168.1.33] port 22.
debug1: Connection established.
debug1: identity file /home/pi/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /home/pi/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/pi/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/pi/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/pi/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/pi/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/pi/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/pi/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.7p1 Raspbian-5+deb8u1
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.9p1 Ubuntu-2ubuntu0.1
debug1: match: OpenSSH_6.9p1 Ubuntu-2ubuntu0.1 pat OpenSSH* compat 0x04000000
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr umac-64-etm@openssh.com zlib@openssh.com
debug1: kex: client->server aes128-ctr umac-64-etm@openssh.com zlib@openssh.com
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ECDSA e1:fc:46:c9:5f:66:f8:d5:8f:13:27:cb:2d:a5:7b:17
debug1: Host '192.168.1.33' is known and matches the ECDSA host key.
debug1: Found key in /home/pi/.ssh/known_hosts:1
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/pi/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug1: Enabling compression at level 6.
debug1: Authentication succeeded (publickey).
Authenticated to 192.168.1.33 ([192.168.1.33]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
debug1: Sending environment.
debug1: Sending env LC_PAPER = fr_FR.UTF-8
debug1: Sending env LC_ADDRESS = fr_FR.UTF-8
debug1: Sending env LC_MONETARY = fr_FR.UTF-8
debug1: Sending env LC_NUMERIC = fr_FR.UTF-8
debug1: Sending env LC_TELEPHONE = fr_FR.UTF-8
debug1: Sending env LC_IDENTIFICATION = fr_FR.UTF-8
debug1: Sending env LANG = en_GB.UTF-8
debug1: Sending env LC_MEASUREMENT = fr_FR.UTF-8
debug1: Sending env LC_TIME = fr_FR.UTF-8
debug1: Sending env LC_NAME = fr_FR.UTF-8
debug1: Sending command: aplay -f dat
Playing WAVE 'stdin' : Signed 16 bit Little Endian, Rate 48000 Hz, Stereo

因为它是在命令行中启动的,所以它正在工作。

编辑: 好的,我想我可能找到了原因:当我的整个 python 脚本在我的远程电脑上用 pygame 启动全屏显示时,我需要用 sudo 启动脚本。因此,我认为当它使用子进程启动 ssh 时,它也尝试在计算机 A 上以 sudo 连接。我认为这是因为我的代码的简单版本正在工作,但如果我以 sudo 启动它,它会要求我密码。问题是,我仍然需要以 sudo 启动脚本。

【问题讨论】:

  • split 引入了不正确拆分的风险——也许这就是原因。要么自己构建序列,要么使用shell=True(适当考虑)。
  • 是的,listenProcess2 = subprocess.Popen('ssh -C a@A aplay -f dat', shell='True', stdin=listenProcess.stdout, stderr=subprocess.PIPE) 提出同样的问题错误
  • 你在那儿引用了 True。我认为它需要是布尔值,而不是字符串
  • listenProcess2 = subprocess.Popen(['ssh', '-C', 'a@A', 'aplay', '-f', 'dat'], stdin=listenProcess.标准输出,标准错误=subprocess.PIPE)
  • 基本检查 - 如果脚本以同一用户身份运行,检查 ssh -v(vv) 输出 - 完成了吗?

标签: python ssh subprocess real-time


【解决方案1】:

警告: 该解决方案可能并不适合所有人,因为它并不完全安全,但对于我的 2 台计算机直接连接而没有互联网连接的特殊问题,风险并不高。

解决方案: 在服务器上,我这样做了:

sudo su - root # log in as root
ssh-keygen -t rsa # generate a rsa key
cat .ssh/id_rsa.pub | ssh b@B 'cat >> .ssh/authorized_keys' # add the key to the other computer
service ssh restart # restart ssh just in case

这样就可以了。感谢您的帮助。

【讨论】:

    猜你喜欢
    • 2020-07-15
    • 1970-01-01
    • 2017-09-10
    • 2019-10-21
    • 2011-05-08
    • 2019-12-08
    • 1970-01-01
    • 2021-12-25
    • 2013-02-16
    相关资源
    最近更新 更多