Skip to content

Instantly share code, notes, and snippets.

@mnofresno
Last active December 19, 2017 12:45
Show Gist options
  • Save mnofresno/7e56ff29288153e7027a271043cba76c to your computer and use it in GitHub Desktop.
Save mnofresno/7e56ff29288153e7027a271043cba76c to your computer and use it in GitHub Desktop.
This Gist is a bash Script that opens ssh-agent and help to avoid inserting private key every day works in Windows/Linux
# Config Vars
private_key_path=/c/Users/myUser/my_private.key
dev_files_path=/d/my/dev/directory
key_expiration_ttl=28800
# Compute config filename with current date
current_date=$(date +"%Y%m%d")
SSH_ENV="$TEMP/environment_$current_date"
# Set up ssh-agent
function start_agent {
echo "Initializing new SSH agent..."
touch $SSH_ENV
chmod 600 "${SSH_ENV}"
/usr/bin/ssh-agent | sed 's/^echo/#echo/' >> "${SSH_ENV}"
. "${SSH_ENV}" > /dev/null
/usr/bin/ssh-add
}
# Source SSH settings, if applicable
if [ -f "${SSH_ENV}" ]; then
. "${SSH_ENV}" > /dev/null
kill -0 $SSH_AGENT_PID 2>/dev/null || {
start_agent
}
else
start_agent
ssh-add -t $key_expiration_ttl $private_key_path
fi
cd $dev_files_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment