Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 6057

Beginners • Setting up scp-->Mac w/o password using sshpass and scp

$
0
0
Hi!

I had this set up and working last year before my SD card died, and now I'm trying to set it up again, and I can't remember how I did it.

I want to set up my Pi to trigger my DSLR via gphoto2 every 10 minutes, to create a timelapse video. My Pi doesn't have much storage space, but I have a Mac on my network that's running 24/7 for other purposes, so I want to scp the files from the Pi to the Mac, automatically, with no user intervention, after it captures each photo.

I have ssh keys set up from the Mac to the Pi already, so I can ssh in to a terminal session without a password, but I can't seem to figure out the reverse.

The command in my script on the Pi is of the form:

Code:

sshpass -f /home/pi/beepass scp -rv /home/pi/jpg/2024-01-28-19-56-22.jpg bee@192.168.1.2:CombLapse/2024-01-28-19-56-22.jpg
where /home/pi/beepass is a text file on the Pi containing the password for bee@192.168.1.2, and 2024-01-28-19-56-22.jpg is the name of the latest photo.

If I run the script, it fails with:

Code:

Executing: program /usr/bin/ssh host 192.168.1.2, user bee, command scp -v -r -t CombLapse/2024-01-28-19-56-22.jpgOpenSSH_8.4p1 Raspbian-5+deb11u3, OpenSSL 1.1.1w  11 Sep 2023debug1: Reading configuration data /etc/ssh/ssh_configdebug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no filesdebug1: /etc/ssh/ssh_config line 21: Applying options for *debug1: Connecting to 192.168.1.2 [192.168.1.2] port 22.debug1: Connection established.debug1: identity file /root/.ssh/id_rsa type 0debug1: identity file /root/.ssh/id_rsa-cert type -1debug1: identity file /root/.ssh/id_dsa type -1debug1: identity file /root/.ssh/id_dsa-cert type -1debug1: identity file /root/.ssh/id_ecdsa type -1debug1: identity file /root/.ssh/id_ecdsa-cert type -1debug1: identity file /root/.ssh/id_ecdsa_sk type -1debug1: identity file /root/.ssh/id_ecdsa_sk-cert type -1debug1: identity file /root/.ssh/id_ed25519 type -1debug1: identity file /root/.ssh/id_ed25519-cert type -1debug1: identity file /root/.ssh/id_ed25519_sk type -1debug1: identity file /root/.ssh/id_ed25519_sk-cert type -1debug1: identity file /root/.ssh/id_xmss type -1debug1: identity file /root/.ssh/id_xmss-cert type -1debug1: Local version string SSH-2.0-OpenSSH_8.4p1 Raspbian-5+deb11u3debug1: Remote protocol version 2.0, remote software version OpenSSH_8.6debug1: match: OpenSSH_8.6 pat OpenSSH* compat 0x04000000debug1: Authenticating to 192.168.1.2:22 as 'bee'debug1: SSH2_MSG_KEXINIT sentdebug1: SSH2_MSG_KEXINIT receiveddebug1: kex: algorithm: curve25519-sha256debug1: kex: host key algorithm: ecdsa-sha2-nistp256debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: nonedebug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: nonedebug1: expecting SSH2_MSG_KEX_ECDH_REPLYdebug1: Server host key: ecdsa-sha2-nistp256 SHA256:<some long string>Host key verification failed.
If I attempt to execute just the scp part from the command line, e.g.:

Code:

scp -rv /home/pi/jpg/2024-01-28-19-56-22.jpg bee@192.168.1.2:CombLapse/2024-01-28-19-56-22.jpg
then I get:

Code:

Executing: program /usr/bin/ssh host 192.168.1.2, user bee, command scp -v -r -t CombLapse/2024-01-28-19-56-22.jpgOpenSSH_8.4p1 Raspbian-5+deb11u3, OpenSSL 1.1.1w  11 Sep 2023debug1: Reading configuration data /etc/ssh/ssh_configdebug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no filesdebug1: /etc/ssh/ssh_config line 21: Applying options for *debug1: Connecting to 192.168.1.2 [192.168.1.2] port 22.debug1: Connection established.debug1: identity file /home/pi/.ssh/id_rsa type -1debug1: identity file /home/pi/.ssh/id_rsa-cert type -1debug1: identity file /home/pi/.ssh/id_dsa type -1debug1: identity file /home/pi/.ssh/id_dsa-cert type -1debug1: identity file /home/pi/.ssh/id_ecdsa type -1debug1: identity file /home/pi/.ssh/id_ecdsa-cert type -1debug1: identity file /home/pi/.ssh/id_ecdsa_sk type -1debug1: identity file /home/pi/.ssh/id_ecdsa_sk-cert type -1debug1: identity file /home/pi/.ssh/id_ed25519 type -1debug1: identity file /home/pi/.ssh/id_ed25519-cert type -1debug1: identity file /home/pi/.ssh/id_ed25519_sk type -1debug1: identity file /home/pi/.ssh/id_ed25519_sk-cert type -1debug1: identity file /home/pi/.ssh/id_xmss type -1debug1: identity file /home/pi/.ssh/id_xmss-cert type -1debug1: Local version string SSH-2.0-OpenSSH_8.4p1 Raspbian-5+deb11u3debug1: Remote protocol version 2.0, remote software version OpenSSH_8.6debug1: match: OpenSSH_8.6 pat OpenSSH* compat 0x04000000debug1: Authenticating to 192.168.1.2:22 as 'bee'debug1: SSH2_MSG_KEXINIT sentdebug1: SSH2_MSG_KEXINIT receiveddebug1: kex: algorithm: curve25519-sha256debug1: kex: host key algorithm: ecdsa-sha2-nistp256debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: nonedebug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: nonedebug1: expecting SSH2_MSG_KEX_ECDH_REPLYdebug1: Server host key: ecdsa-sha2-nistp256 SHA256:<some long string>The authenticity of host '192.168.1.2 (192.168.1.2)' can't be established.ECDSA key fingerprint is SHA256:<some long string>.Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
where "yes" is my typed response, and then I get:

Code:

Failed to add the host to the list of known hosts (/home/pi/.ssh/known_hosts).debug1: rekey out after 134217728 blocksdebug1: SSH2_MSG_NEWKEYS sentdebug1: expecting SSH2_MSG_NEWKEYSdebug1: SSH2_MSG_NEWKEYS receiveddebug1: rekey in after 134217728 blocksdebug1: Will attempt key: /home/pi/.ssh/id_rsa debug1: Will attempt key: /home/pi/.ssh/id_dsa debug1: Will attempt key: /home/pi/.ssh/id_ecdsa debug1: Will attempt key: /home/pi/.ssh/id_ecdsa_sk debug1: Will attempt key: /home/pi/.ssh/id_ed25519 debug1: Will attempt key: /home/pi/.ssh/id_ed25519_sk debug1: Will attempt key: /home/pi/.ssh/id_xmss debug1: SSH2_MSG_EXT_INFO receiveddebug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,sk-ssh-ed25519@openssh.com,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,sk-ecdsa-sha2-nistp256@openssh.com,webauthn-sk-ecdsa-sha2-nistp256@openssh.com>debug1: SSH2_MSG_SERVICE_ACCEPT receiveddebug1: Authentications that can continue: publickey,password,keyboard-interactivedebug1: Next authentication method: publickeydebug1: Trying private key: /home/pi/.ssh/id_rsadebug1: Trying private key: /home/pi/.ssh/id_dsadebug1: Trying private key: /home/pi/.ssh/id_ecdsadebug1: Trying private key: /home/pi/.ssh/id_ecdsa_skdebug1: Trying private key: /home/pi/.ssh/id_ed25519debug1: Trying private key: /home/pi/.ssh/id_ed25519_skdebug1: Trying private key: /home/pi/.ssh/id_xmssdebug1: Next authentication method: keyboard-interactivePassword:
I copy/paste the password from beepass, which succeeds as:

Code:

debug1: Authentication succeeded (keyboard-interactive).Authenticated to 192.168.1.2 ([192.168.1.2]:22).debug1: channel 0: new [client-session]debug1: Requesting no-more-sessions@openssh.comdebug1: Entering interactive session.debug1: pledge: networkdebug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0debug1: Sending environment.debug1: Sending env LANG = en_GB.UTF-8debug1: Sending command: scp -v -r -t CombLapse/2024-01-28-19-56-22.jpgSending file modes: C0644 1323521 2024-01-28-19-56-22.jpgSink: C0644 1323521 2024-01-28-19-56-22.jpg2024-01-28-19-56-22.jpg                                                                        0%    0     0.0KB/s   --:-- ETA2024-01-28-19-56-22.jpg                                                                      100% 1293KB 820.3KB/s   00:01    debug1: client_input_channel_req: channel 0 rtype exit-status reply 0debug1: channel 0: free: client-session, nchannels 1debug1: fd 0 clearing O_NONBLOCKTransferred: sent 1326768, received 2852 bytes, in 1.7 secondsBytes per second: sent 792378.7, received 1703.3debug1: Exit status 0
resulting in a viewable file at: /Users/bee/CombLapse/2024-01-28-19-56-22.jpg on the Mac.

So I have the user/pass correct for bee@192.168.1.2, and the file is correct; I just have sshpass and/or the keys set up incorrectly.

When I run ssh-keygen on the Pi, I get:

Code:

pi@PILapse:~/.ssh $ sudo ssh-keygenGenerating public/private rsa key pair.Enter file in which to save the key (/root/.ssh/id_rsa): .ssh/ida_rsaEnter passphrase (empty for no passphrase): Enter same passphrase again: Saving key ".ssh/ida_rsa" failed: No such file or directory
which doesn't make sense because I'm in .ssh/ already, so it must exist, and it currently contains:

Code:

pi@PILapse:~/.ssh $ lsauthorized_keys
so I ran it again and saved it to a tmp dir:

Code:

Generating public/private rsa key pair.Enter file in which to save the key (/home/pi/.ssh/id_rsa): /home/pi/tmp/id_rsaEnter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/pi/tmp/id_rsaYour public key has been saved in /home/pi/tmp/id_rsa.pubThe key fingerprint is:SHA256:<a long string> 
then moved the files:

Code:

pi@PILapse:~/tmp $ sudo mv id_rsa.pub ~/.sshpi@PILapse:~/tmp $ sudo mv id_rsa ~/.sshpi@PILapse:~/tmp $ cd ~/.sshpi@PILapse:~/.ssh $ lsauthorized_keys  id_rsa  id_rsa.pub
So now I have what appears to be a key in the correct place:

Code:

pi@PILapse:~/.ssh $ cat id_rsa.pubssh-rsa <a really long string> pi@PILapse
.

which I copied to the bee dir on my mac:

Code:

steven@Stevens-Mac-mini / % sudo cat /Users/Bee/.ssh/authorized_keysssh-rsa <the same really long string> pi@PILapse
but scp still generates the same "The authenticity of host '192.168.1.2 (192.168.1.2)' can't be established." error and prompts me for a password.

Do I have a really silly, obvious syntax error somewhere?

Thanks!

Statistics: Posted by TheWaterbug — Mon Jan 29, 2024 5:04 am



Viewing all articles
Browse latest Browse all 6057

Trending Articles