Created
March 26, 2014 14:28
-
-
Save andrewnicols/9784621 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Sensible defaults. | |
targettime="8pm" | |
keyfile="$HOME"/.ssh/id_rsa | |
privatekeydir="" | |
publickeydir="" | |
reporoot="$HOME"/git | |
# Define the default tunnels. | |
declare -A tunnels | |
tunnels["cerebro"]="fastissforward" | |
tunnels["kcol"]="fastcisforward" | |
# Now try and load any user config. | |
configfile="$HOME/.sshadd" | |
if [ -f "$configfile" ] | |
then | |
source "$configfile" | |
fi | |
# And handle private key directories. | |
if [ -n "$publickeydir" ] | |
then | |
keyfile="$privatekeydir"/* | |
keymatch=`ssh-keygen -l -f "$publickeydir"/* | head -1 | awk '{print $2}'` | |
else | |
keymatch=`ssh-keygen -l -f "$keyfile" | awk '{print $2}'` | |
fi | |
target=$(date -d $targettime +%s) | |
seconds=$(($target-`date +%s`)) | |
if [ "$seconds" -lt 0 ] | |
then | |
seconds=$((60*60*1)) | |
fi | |
ssh-add -l | grep "$keymatch" > /dev/null | |
if [ "$?" -ne 0 ] | |
then | |
echo "Adding ssh key" | |
ssh-add -t $seconds $keyfile | |
else | |
echo "Key already loaded in keychain" | |
echo "* Not adding again" | |
fi | |
# Check that we're not generating for a time in the past | |
now=$(date +%s) | |
if [ $now -gt $target ] | |
then | |
# Must be working in the evening. Only go for 1 hour | |
seconds=$((60*60*1)) | |
echo "Maintaining session for one hour - go to bed" | |
else | |
echo "Maintaining session until $targettime" | |
fi | |
function maketunnel { | |
tunnelserver=$1 | |
repo=$2 | |
echo | |
ps -ef | grep "$tunnelserver" | grep -v grep > /dev/null | |
if [ "$?" -ne 0 ] | |
then | |
echo "Re-generating $repo tunnel configuration" | |
"$reporoot"/"$repo"/bin/sshtunnelcfggen | |
echo "Connecting to $repo tunnel" | |
ssh "$tunnelserver" -f sleep $(($seconds)) | |
else | |
echo "$repo tunnel currently connected." | |
echo "* Not re-connecting tunnel" | |
echo "* Not re-generating configuration" | |
fi | |
} | |
# Connect to the various tunnels. | |
for repository in "${!tunnels[@]}" | |
do | |
maketunnel ${tunnels["$repository"]} "$repository" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment