I've got two computers running Linux a@A and b@B, a microphone being connected to B. Using A, I log in into b@B to launch a python script which has the possibility to listen to the microphone in real time.
Here is my code :
#!/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)
If I launch the command:
arecord -D plughw:1,0 -f dat | ssh -C a@A aplay -f dat
it works perfectly fine and I can listen to what is recorded by the microphone. But if it is from my python script, it raises the following error:
Host key verification failed
I've tried to "clean" the known_hosts file as suggested when the error is raised. I've also tried paramiko but does not seem to be my solution.
I've ran out of ideas.
Thanks
EDIT:
Running arecord -D plughw:1,0 -f dat | ssh -v -C [email protected] aplay -f dat
displays :
pi@raspberrypi ~/Documents $ arecord -D plughw:1,0 -f dat | ssh -v -C [email protected] 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 [email protected] [email protected]
debug1: kex: client->server aes128-ctr [email protected] [email protected]
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 [email protected]
debug1: Entering interactive session.
debug1: client_input_global_request: rtype [email protected] 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
As it is launched in command line, it is working.
EDIT: Ok, I think I might have found the reason: As my whole python script launches a fullscreen display with pygame on my remote pc, I need to launch the script with sudo. Thus I think when it launches ssh with subprocess, it is also trying to connect as sudo on computer A. The reason I think that is that the simple version of my code is working, but if I launch it as sudo, it asks for my password. Problem is, I still need to launch the script as sudo.
Warning: The solution might not be for everyone as it is not totally safe but for my particular problem where my 2 computers are directly connected without internet connection, the risk is not high.
Solution: On the server, I did this:
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
This way it's working. Thanks for your help.