Skip to content

Instantly share code, notes, and snippets.

@unacceptable
Last active July 14, 2017 02:26
Show Gist options
  • Save unacceptable/2eea19144789627e6b9a25b0c043cb9b to your computer and use it in GitHub Desktop.
Save unacceptable/2eea19144789627e6b9a25b0c043cb9b to your computer and use it in GitHub Desktop.

Daedalus

Named after the father of Icarus, Daedalus allows modern day craftsmen to easily connect to their servers via the hostname or some other friendly identifier without having to remember IP addresses, usernames or ssh key locations.

Example usage

Daedalus hostname

NOTE

This is a work in progress and a lot of functionality still has yet to be added.

#!/bin/bash
set -e
# Written by: Robert J.
#######################################
### Info Function #####################
#######################################
db_info(){
DB_SSH_USR=user
DB_IP=123.45.67.89
DB_SSH_PORT=22
DB_SSH_KEY="$HOME/.ssh/id_rsa.pem"
TABLE="some_db.some_table"
return;
};
#######################################
### Main Function #####################
#######################################
main_function(){
db_info;
helpcheck $@;
# Fetch necessary information from Heimdall Database
HOSTNAME="$1"
[[ "$HOSTNAME" == "Daedalus" ]] && {
print "Connecting to Daedalus Server" yellow --nosep
sshf "${DB_SSH_USR}" "${DB_IP}" "${DB_SSH_KEY}" "${DB_SSH_PORT}"
die "Terminating session, preserving ssh key." 0;
}
# retrieve information from DB about specified server
db_fetch "$HOSTNAME";
# parse retrieved information
parse_info;
sshf "${SSH_USR}" "${IP}" "/tmp/${HOSTNAME}.pem" "${SSH_PORT}"
rm -rf "/tmp/${HOSTNAME}.pem" ||
die "Unable to destroy key" 254;
ls -la "/tmp/${HOSTNAME}.pem" 2> /dev/null ||
print "Local Key Destroyed" yellow --nosep;
};
#######################################
### Generic Functions #################
#######################################
helpcheck(){
for arg in "$@"; do
[[ "$arg" == "--help" || "$arg" == "-h" ]] && {
disphelp;
die "" 0 --nosep
};
done
[[ -z "${1}" ]] && {
disphelp &&
die "Invalid Syntax" 1;
} || {
return 0;
};
};
print(){
# opted against $_ and $! for this usage
last="${!#}"
output="$1";
size="${#output}";
# Colors pulled from ansi-color script
######################################
case "$2" in
red)
color="196"
;;
orange)
color="202"
;;
yellow)
color="226"
;;
green)
color="46"
;;
blue)
color="27"
;;
indigo)
color="21"
;;
violet)
color="54"
;;
grey)
color="8"
;;
*)
color="0"
;;
esac
printf -v sep "%${size}s" "-";
[[ "$last" == "--nosep" ]] && {
printf "\e[38;5;%dm%s\e[0m\n" "$color" "$output";
return;
};
printf "\n\e[38;5;%dm%s\e[0m\n%s\n" "$color" "$output" "${sep// /-}";
};
die(){
message="${1}"
exit_code="${2}"
[[ -z "$exit_code" ]] && {
exit_code=1;
};
# determine if exit code is an integer
[[ -z ${exit_code//[0-9]/} ]] || {
print "Invalid exit code specified in script: $exit_code\n" yellow --nosep
exit_code=255;
};
[[ "$exit_code" -gt 0 ]] && {
print "Error Encountered:" "red";
};
print "${message}" "yellow" "--nosep";
exit "$exit_code";
};
#######################################
### Program Specific Functions ########
#######################################
disphelp(){
print "Command Documentation" green --nosep
print "Usage Examples:" yellow;
printf "\t%s <HOSTNAME> \n\t# Displays this table for reference\n\n" "${0##*/}"
printf "\t%s --help | -h \n\t# Displays this table for reference\n\n" "${0##*/}"
print "Current hostname list:" orange
db_parse "hostname"
print "Options:" yellow;
printf "\t--help | -h \t# Displays this table for reference\n"
}
sshf(){
SSH_USR="$1"
IP="$2"
SSH_KEY="$3"
SSH_PORT="$4"
SSH_COMMAND="$5"
[[ "$(cat "$SSH_KEY")" == "NULL" ]] && {
ssh "${SSH_USR}"@"${IP}" -p "${SSH_PORT}" "${SSH_COMMAND}"
return;
};
# ssh to server
ssh -i "${SSH_KEY}" "${SSH_USR}"@"${IP}" -p "${SSH_PORT}" "${SSH_COMMAND}"
};
db_parse(){
SQL_QUERY="$1"
MATCH="$2"
# Need to inject literal quotes into SQL Command for running from BASH
[[ -z "$MATCH" ]] && {
SQL="\"select ${SQL_QUERY} from ${TABLE} ; \""
} || {
SQL="\"select ${SQL_QUERY} from ${TABLE} where ${MATCH} ;\""
};
MYSQL_COMMAND="sudo mysql -u root -s -N -e ${SQL}"
sshf "${DB_SSH_USR}" "${DB_IP}" "${DB_SSH_KEY}" "${DB_SSH_PORT}" "${MYSQL_COMMAND}"
}
db_fetch(){
INFO=$(
db_parse "username,ip,port" "hostname='${HOSTNAME}'"
)
[[ -z $INFO ]] && {
die "Unable to find \'${HOSTNAME}\' in Heimdall DB.";
}
SSH_KEY=$(
echo -e "$(
db_parse "ssh_key" "hostname='${HOSTNAME}'"
)"
)
};
parse_info(){
SSH_USR=$( echo "$INFO" | awk '{print $1}')
IP=$( echo "$INFO" | awk '{print $2}')
SSH_PORT=$( echo "$INFO" | awk '{print $3}')
echo "$SSH_KEY" > "/tmp/${HOSTNAME}.pem";
SSH_KEY="/tmp/${HOSTNAME}.pem";
chmod 600 "$SSH_KEY"
# Print info for user knowledge
print "Username: $SSH_USR" yellow --nosep
print "IP: $IP" yellow --nosep
print "Port: $SSH_PORT" yellow --nosep
print "SSH Key File: $SSH_KEY" yellow --nosep
}
#######################################
### Execution #########################
#######################################
# variable deliberately not quoted to keep arguments independent
main_function $@;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment