Skip to content

Instantly share code, notes, and snippets.

@tshabatyn
Last active August 13, 2020 15:25
Show Gist options
  • Save tshabatyn/abebacd981fb91c7ea55d05f9631899e to your computer and use it in GitHub Desktop.
Save tshabatyn/abebacd981fb91c7ea55d05f9631899e to your computer and use it in GitHub Desktop.
Generate ~/.ssh/conf from DigitalOcean API call
#!/bin/sh
TOKEN=$PERSONAL_ACCESS_TOKEN # you can get TOKEN by the link https://cloud.digitalocean.com/account/api
# save droplets list to /tmp/respons.txt
curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" "https://api.digitalocean.com/v2/droplets?status=active&page=1&per_page=50" > /tmp/respons.txt
# generate new ~/.ssh/conf from the list of droplets
cat /tmp/respons.txt | jq -s -c 'sort_by(.name) | .[]' | jq -r '.droplets|=sort_by(.name)|.droplets[]|{name: .name, ip_address: .networks.v4[1].ip_address, ip_address_privat: .networks.v4[0].ip_address}|"Host\t\t\t\te-\(.name)\n\tHostName\t\t\(.ip_address)\n## PrivateIp:\t\t\(.ip_address_privat)\n\tUser\t\t\troot\n\tAddKeysToAgent\tyes\n\tForwardAgent\tyes\n\tForwardX11\t\tyes\n\tUseKeychain\t\tyes\n\tIdentityFile\t~/.ssh/id_rsa\n"' > ./do_conf
sed -i -e 's/^Host\t\t\t\t\(.*\)/Host\t\t\t\t\L\1/' ./do_conf
# check all the cronjobs on all DigitalOcean droplets.
rm validation.sh && touch validation.sh && chmod +x validation.sh
echo '#!/bin/sh' > validation.sh
echo >> validation.sh
cat /tmp/respons.txt | jq -r '.droplets|=sort_by(.name)|.droplets[]|{name: .name, ip_address: .networks.v4[0].ip_address}|"ssh -i ~/.ssh/id_rsa root@\(.ip_address) \"hostname && ls -lA /var/spool/cron/crontabs/* && cat /var/spool/cron/crontabs/* | grep -v \\\"^$\\\" | grep -v \\\"^#\\\" | grep -v \\\"bin/magento\\\"\""' >> validation.sh
rm /tmp/respons.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment