Skip to content

Instantly share code, notes, and snippets.

@jonathanselander
Created March 14, 2017 13:28
Show Gist options
  • Save jonathanselander/766e8b7d7ed863f763226898e879005f to your computer and use it in GitHub Desktop.
Save jonathanselander/766e8b7d7ed863f763226898e879005f to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Script that performs an API lookup to the Cloudnet server
# registry with an API key found in $HOME/.cloudnet.settings unless
# changed.
#
# After a server is selected, an SSH session is opened with a username
# found in the settings file. The file is a bash file that's included
# further down and can look like this:
#
# API_KEY=your_api_key
# SSH_USERNAME=deploy
#
SETTINGS=$HOME/.cloudnet.settings
if ! [[ -f $SETTINGS ]]
then
echo Settings file not found in "$SETTINGS"
exit 1
fi
. $SETTINGS
CLOUDNET_API_URL="https://api.cloudnet.se/api/v2/instance/?api_key=$API_KEY"
IFS=$'\n'
OPTIONS=($(curl -s $CLOUDNET_API_URL 2>/dev/null | \
python -c '
import sys, json
serverList = json.load(sys.stdin)
for server in serverList:
print server["description"] + "|" + server["ip"]
'))
OPTIONS=($(sort -t'|' -k3 <<<"${OPTIONS[*]}"))
unset IFS
for (( i=0; i < ${#OPTIONS[@]}; i++ ))
do
echo "$i) ${OPTIONS[i]}" | sed 's/|/ - /g'
done
read -p "Choose server to SSH into: " choice
IFS="|"
SERVER=(${OPTIONS[$choice]})
ssh $SSH_USERNAME@${SERVER[1]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment