Skip to content

Instantly share code, notes, and snippets.

@aaronstpierre
Created November 16, 2024 19:00
Show Gist options
  • Save aaronstpierre/16d9a838416f9b9de189ed7afdb0716a to your computer and use it in GitHub Desktop.
Save aaronstpierre/16d9a838416f9b9de189ed7afdb0716a to your computer and use it in GitHub Desktop.
add ssh key to all sites on Rocket Dot Net
#!/bin/bash
#
# rdn_add_ssh_key:
#
# Add SSH key to all sites on a rocket.net host. Doesn't check for errors (mostly) just blindly attempts to add
# everywhere. Will not add if the key is already there.
#
# Variables
API_BASE_URL="https://api.rocket.net"
API_VERSION="v1"
API_KEY="" # Bearer Token see https://rocketdotnet.readme.io/reference/sites-apiopenapi_servercontrollersauthentication_controllerlogin_post
SSH_KEY="" # Replace with your actual SSH key
# Functions
get_all_sites() {
curl -s -X GET "$API_BASE_URL/$API_VERSION/sites?page=1&per_page=1000" \
-H "Authorization: Bearer $API_KEY" \
-H "accept: application/json" \
-H "Content-Type: application/json" | jq -r '.result[].id'
}
add_ssh_key_to_site() {
local site_id=$1
curl -s -X POST "$API_BASE_URL/$API_VERSION/sites/$site_id/ssh/keys" \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d "{\"name\": \"asp\", \"key\": \"$SSH_KEY\"}"
}
# Main Script
echo "Fetching all site IDs..."
site_ids=$(get_all_sites)
if [[ -z "$site_ids" ]]; then
echo "No sites found or failed to fetch sites. Exiting."
exit 1
fi
echo "Adding SSH key to all sites..."
for site_id in $site_ids; do
echo "Adding SSH key to site: $site_id"
response=$(add_ssh_key_to_site "$site_id")
if echo "$response" | grep -q '"success":true'; then
echo "SSH key added successfully to site $site_id."
else
echo "Failed to add SSH key to site $site_id. Response: $response"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment