Skip to content

Instantly share code, notes, and snippets.

@Dzordzu
Last active July 4, 2019 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dzordzu/b89999aa0c503e6957d06c075d4edc5d to your computer and use it in GitHub Desktop.
Save Dzordzu/b89999aa0c503e6957d06c075d4edc5d to your computer and use it in GitHub Desktop.
SSH key auto add

SSH Key Auto Add

Note

Use ansible instead

WARNING!

It uses eval

Description

Appends (if not already added) a line with PUBLIC key to every of the given servers

Must include (in the same dir)

  • list - file with servers (line per server)
  • ssh-key - key that will be added

Dependencies

  • ssh
  • ssh-key
  • cat
  • grep
test@dev.example.com
root@dev.example.com
test@prod.example.com
#!/bin/bash
eval `ssh-agent -s` && ssh-add ~/.ssh/id_rsa
exec_command=$(cat <<-END
mkdir -p ~/.ssh/
touch ~/.ssh/authorized_keys
if grep -Fxq "$(cat ssh-key)" ~/.ssh/authorized_keys
then
echo "Already exists"
else
echo "$(cat ssh-key)" >> ~/.ssh/authorized_keys
fi
END
)
while read -r line; do
echo "Connecting to" $line
eval "ssh" $line "'$exec_command'" "< /dev/null"
echo ""
# echo -e "" >> test.txt
# ssh $line ''
done < list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment