I'm writing a script to creating a set off ssh keys and uploading them to the known hosts. This is for when you want keys per device.
I included an option to upload the new key to the host after its creation. However, bash seems to have lost my session or something, because it prompts me for a password, when it should have just sent the item since I have the keys loaded in a key manager, or as a fall back, prompt me for the password of the keyfile specified in the ssh config.
Here's the relevant parts of the script:
for host in "${hosts[@]}"
do
keyfile=$(printf "%s_keys/%s_%s_%s.id_ed25519" "$system" "$system" "$email" "$host")
ssh-keygen -t ed25519 -C $(printf "%s_%s" "$email" "$system") -P "$pw1" -f "$keyfile"
read -r -p "ssh new key to host $host? [y/n] " response
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]
then
cat $keyfile.pub | ssh "$host" 'cat >> .ssh/authorized_keys'
fi
done
So when I ran the script, I got this:
The key fingerprint is:
SHA256:n8FWwe+TaHbWKwWVzCdIbAcfFnPTh8J0pukzuv9J4pQ a_b
The key's randomart image is:
+--[ED25519 256]--+
...
+----[SHA256]-----+
ssh new key to host [email protected]? [y/n] y
u[email protected]'s password:
Permission denied, please try again.
However:
$ ssh myhost.com
You have new mail.
Last login: Sun Oct 30 03:28:07 2016 from 65.60.221.155
[myhost.com ~]
As a test, I wrote this more simplified script, and it fired without a hitch:
$ cat test.bash
#!/bin/bash
echo test | ssh myhost.com 'cat >> test.txt'
$ ./test.bash
$ ssh myhost.com
You have new mail.
Last login: Sun Oct 30 03:33:25 2016 from 65.60.221.155
[myhost.com ~] cat test.txt
test
What did I do in my longer script that caused it to forget my ssh information?
ssh-copy-id -i ~/.ssh/<pub_key> user@server
wouldn't that make it easier?-vvv
).ssh myhost.com
on the command line butssh [email protected]
in the script. You may be attempting to log in as a different user.cat >> test
andcat >> somedirthatmaynotexist/test
are two very different things.ssh/config
and defaulting to user/pass login. What you're referring to is the output, not the command in the script. The script hascat $keyfile.pub | ssh "$host" 'cat >> .ssh/authorized_keys'