Skip to content

Instantly share code, notes, and snippets.

@furquanuddin94
Last active April 26, 2019 10:51
Show Gist options
  • Save furquanuddin94/0b835e4e33fcfcca526b17f8c5353616 to your computer and use it in GitHub Desktop.
Save furquanuddin94/0b835e4e33fcfcca526b17f8c5353616 to your computer and use it in GitHub Desktop.
Utility to access server boxes (over SSH) via IP or ELB (specifically for AWS).
#!/bin/bash
#Script to access server boxes
#Put the script in any directory present on system's $PATH. Make the file executable via "chmod +x portkey". Then use "portkey help" for instructions on how to use.
#Prerequirements: Install JQ and Sponge
homeDir=$( echo $HOME )
configFile="${homeDir}/portkey.conf"
paramCount="$#"
if [ "$paramCount" = "0" ] || [ "$1" = "help" ]
then
tput setaf 3; echo -e "PORTKEY: A utility to easily access server boxes."
tput setaf 3; echo -e "Named after the Harry Potter artefact, an object enchanted to instantly bring anyone touching it to a specific location!"
tput setaf 1; echo -e "\nCommands:"
tput setaf 2; echo -e "\nportkey init :"
tput setaf 7; echo -e "Command to be executed only once while setting up. Installs necessary utilities. Also asks for ssh username (example: abc_123) and an AWS Machine IP on which you already have access (This is required for fetching instances under ELBs)."
tput setaf 2; echo -e "\nportkey set <arg1> <arg2> ..... <argn> \"ip\"/\"elb\" <ip value/elb name> :"
tput setaf 7; echo -e "Setter command to construct the map. Second last argument must be either \"ip\" or \"elb\". Last argument must be the value of ip or elb. Apart from that, the map can be as nested as possible."
tput setaf 3; echo -e "Sample usage:"
tput setaf 7; echo -e "portkey set fulfillment dev1 ip 1.2.3.4"
tput setaf 7; echo -e "portkey set fulfillment prod elb abcd"
tput setaf 2; echo -e "\nportkey get <arg1> <arg2> ..... <argn>:"
tput setaf 7; echo -e "Getter command to view the stored map. Last argument should not be IP or ELB."
tput setaf 3; echo -e "Sample usage:"
tput setaf 7; echo -e "portkey get (prints complete map)"
tput setaf 7; echo -e "portkey get fulfillment (prints fulfillment service details with all its nested IPs/ELBs)"
tput setaf 7; echo -e "portkey get fulfillment dev1 (prints ip/elb assigned to dev1 of fulfillment service)"
tput setaf 2; echo -e "\nportkey del <arg1> <arg2> ..... <argn>:"
tput setaf 7; echo -e "Delete command to remove entries from the stored map."
tput setaf 3; echo -e "Sample usage:"
tput setaf 7; echo -e "portkey del fulfillment (delete fulfillment service details with all its nested IPs/ELBs)"
tput setaf 7; echo -e "portkey del fulfillment dev1 (delete ip/elb assigned to dev1 of fulfillment service)"
tput setaf 2; echo -e "\nportkey <arg1> <arg2> ..... <argn>:"
tput setaf 7; echo -e "Command to access server boxes. If the arguments point to an IP, it simply accesses the box else if its an ELB, first it fetches the instances of the ELB and the user can then choose the desired box to access."
tput setaf 3; echo -e "Sample usage:"
tput setaf 7; echo -e "portkey fulfillment dev1 (To access dev1 of fulfillment service)"
tput setaf 7; echo -e "portkey fulfillment prod (To fetch boxes under prod ELB of fulfillment service and access one of them)"
tput setaf 2; echo -e "\nportkey ip <ip value> :"
tput setaf 7; echo -e "Command to access a server box directly via IP"
tput setaf 3; echo -e "Sample usage:"
tput setaf 7; echo -e "portkey ip 1.2.3.4 (To access box with IP 1.2.3.4)"
exit 0
elif [ "$1" = "init" ]
then
touch "$configFile"
chmod +w "$configFile"
echo "{}" > $configFile
echo -e "\n"
read -p "Enter username: " username
echo -e "\n"
read -p "Select IP of an AWS machine on which you have access (for fetching details related to ELBs): " machineIP
jq ".username = \"${username}\"" ${configFile} | sponge ${configFile}
jq ".defaultMachine.ip = \"${machineIP}\"" ${configFile} | sponge ${configFile}
echo -e "Config setup complete!"
exit 0
fi
username="$( jq -r '.username' ${configFile} )"
machineIP="$( jq -r '.defaultMachine.ip' ${configFile} )"
secondLast="$(($paramCount-1))"
lastParam="${!paramCount}"
secLastParam="${!secondLast}"
sshTo () {
ip="$1"
echo "IP: "${ip}
ssh -F /dev/null ${username}@${ip}
}
if [ "$1" = "ip" ]
then
boxIP="$2"
sshTo $boxIP
elif [ "$1" = "get" ] && [ "$lastParam" != "ip" ] && [ "$lastParam" != "elb" ]
then
path=""
for (( i = 2; i <= $paramCount; i++ ))
do
path="$path.${!i}"
done
if [ "$path" = "" ]
then
path="."
fi
echo "$( jq -r $path ${configFile} )" | jq '.'
elif [ "$secLastParam" = "ip" ] || [ "$secLastParam" = "elb" ] && [ "$1" = "set" ] && [ "$lastParam" != "null" ]
then
path=""
for (( i = 2; i <= $paramCount-2; i++ ))
do
path="$path.${!i}"
done
type=$secLastParam
newVal=$lastParam
jq "del($path)" ${configFile} | sponge ${configFile}
if [ $type = "ip" ]
then
jq "$path.ip = \"${newVal}\"" ${configFile} | sponge ${configFile}
else
jq "$path.elb = \"${newVal}\"" ${configFile} | sponge ${configFile}
fi
elif [ "$1" = "del" ] && [ "$paramCount" -gt 1 ] && [ "$lastParam" != "ip" ] && [ "$lastParam" != "elb" ] && [ "$2" != "username" ]
then
path=""
for (( i = 2; i <= $paramCount; i++ ))
do
path="$path.${!i}"
done
jq "del($path)" ${configFile} | sponge ${configFile}
else
path=""
for (( i = 1; i <= $paramCount; i++ ))
do
path="$path.${!i}"
done
ip="$( jq -r "$path.ip" ${configFile} )"
elbName="$( jq -r "$path.elb" ${configFile} )"
if [ "$ip" != "null" ]
then
sshTo $ip
elif [ "$elbName" != "null" ]
then
echo "ELB Name: " $elbName
elbDetailsCmd="aws elb describe-load-balancers --load-balancer-names $elbName"
elbDetails="$( ssh -F /dev/null ${username}@${machineIP} "$elbDetailsCmd" 2>&1 )"
instanceList="$( echo "$elbDetails" | grep "InstanceId" | awk '{print $2}' )"
instanceDetailsCmd="aws ec2 describe-instances --instance-ids"
while read -r instanceId; do
instanceDetailsCmd="$instanceDetailsCmd $instanceId"
done <<< "$instanceList"
instanceDetails="$( ssh -F /dev/null ${username}@${machineIP} "$instanceDetailsCmd" 2>&1 )"
ipList="$( echo "$instanceDetails" | grep -w "PrivateIpAddress" | awk '{print $2}' | tr -d ',' | sort -u )"
lineNo=1;
echo -e "\nInstance List:\n"
while read -r ip; do
echo "($lineNo) $ip"
((lineNo++))
done <<< "$ipList"
echo -e "\n"
read -p "Select an instance: " lineNo
ip="$( echo "$ipList" | sed "${lineNo}q;d" | tr -d '\"')"
sshTo $ip
else
echo "Invalid command!"
fi
fi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment