This shell command isn’t working:
ssh root@IP "if '$(cat ~/.ssh/authorized_keys | grep $KEY)' == '';then;echo $KEY >> ~/.ssh/authorized_keys;done"
$KEY contains my public RSA key. What I’m trying to do is to check if my key has been added to the authorized_keys file, if not, then to add it. IP of course is replaced with a real ip address.
Any idea what I’m doing wrong?
Edit:
In case anyone is curious, this is what I was doing:
#!/bin/sh
# Get your RSA key.
KEY=""
for line in $(cat ~/.ssh/id_rsa.pub)
do
KEY="$KEY $line"
done
# Add your RSA key to the machine's authorized_keys if it's not already there.
ssh root@$1 "grep -q '$KEY' ~/.ssh/authorized_keys || echo '$KEY' >> ~/.ssh/authorized_keys"
# Connect to the machine.
ssh root@$1
The idea was to ssh into a machine (using this script) and not have to enter the password in the next time I log in. The IP address is passed as a command line argument.
You expand
$()thingie on the client side. (at least).looks like a shorter way to do the same.