newSSHKey 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #!/bin/bash
  2. KEY_NAME="${1:?need a SSHKey name from /me}"
  3. KEY_DIR="${HOME}/.ssh/keys.d"
  4. # check if sshkey exists locally
  5. if [ -e "${KEY_DIR}/${KEY_NAME}" ] ; then
  6. echo "SSH Key ${KEY_DIR}/${KEY_NAME} already exists locally"
  7. else
  8. echo "SSH Key ${KEY_DIR}/${KEY_NAME} not found locally"
  9. echo ">>> Creating it ..."
  10. ssh-keygen -t ed25519 -f "${KEY_DIR}/${KEY_NAME}" -C "OVH_${KEY_NAME}" -q -N ""
  11. delSSHKey "${KEY_NAME}"
  12. fi
  13. PUBKEY="$( cat "${KEY_DIR}/${KEY_NAME}.pub" )"
  14. # check if KEY_NAME exists into OVH User Account /me
  15. # remote_keyname = "string or empty"
  16. remote_keyname=$( ovhcli get "me/sshKey" | jq --arg v "${KEY_NAME}" -r '.|to_entries[]|select(.value == $v ).value' )
  17. # if remote_keyname exists check if pubkey is matching.
  18. remote_pubkey=$( ovhcli get "me/sshKey/${KEY_NAME}" | jq -r '.key' )
  19. # if remote_keyname does not match KEY_NAME = this KEY_NAME is not already uploaded into OVH Account
  20. if [ -z "${remote_keyname}" ] || [ "${remote_pubkey}" != "${PUBKEY}" ] ; then
  21. echo "SSH Keys does not exists remotely"
  22. echo ">>> Uploading ${KEY_DIR}/${KEY_NAME} to OVH account"
  23. HTTP_PAYLOAD='{ "key": "'${PUBKEY}'", "keyName": "'${KEY_NAME}'" }'
  24. ovhcli post "me/sshKey" "${HTTP_PAYLOAD}" | jq -r 'select(. != null)'
  25. else
  26. echo "SSH Keys are up-to-date"
  27. fi
  28. exit $?