簡體   English   中英

如果使用子進程運行,ssh 主機密鑰驗證失敗

[英]ssh host key verification failed if run with subprocess

我有兩台運行 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 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 身份啟動腳本。

警告:該解決方案可能並不適合所有人,因為它不是完全安全的,但對於我的 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

這樣它就起作用了。 謝謝你的幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM