Skip to content

Instantly share code, notes, and snippets.

@hewersonfreitas
Last active January 31, 2020 16:14
Show Gist options
  • Save hewersonfreitas/74f2657689f1d204c441fe8d484d38d6 to your computer and use it in GitHub Desktop.
Save hewersonfreitas/74f2657689f1d204c441fe8d484d38d6 to your computer and use it in GitHub Desktop.
Scrip for write public_key in Droplets Digital Ocean
#!/bin/bash
####
# Install Requirements
####
sudo apt-get install jq -y
########
## Envirioment @var TOKEN in OS Linux
########
echo "write TOKEN Digital Ocean for auth connection API"
read TOKEN
# get IPs Droplets by TAGS
JSON=$(curl -X GET -H "Content-Type: application/json" -H \
"Authorization: Bearer ${TOKEN}" \
"https://api.digitalocean.com/v2/droplets")
echo "write path and name ssh public key"
read path_public_key
for IPS in $(echo "$JSON" | jq -r ".droplets | .[].networks.v4 | .[0].ip_address")
do
if [ -z ${HOSTS} ]; then
HOSTS="${IPS}"
else
HOSTS="${IPS},${HOSTS}"
fi
done
echo $HOSTS | sed -n 1'p' | tr ',' '\n' | while read host; do
cat ${path_public_key} | ssh root@${host} 'cat >> .ssh/authorized_keys'
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment