Last active
December 5, 2016 13:59
-
-
Save plytro/aed5a0d1d5664367b859 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 | |
# | |
# File: ~/.ssh/ssh-agent-setup | |
# | |
# Checks authentication environment. | |
# If the ssh-agent is not running, starts a new one. | |
# | |
# Setup instructions: | |
# | |
# 1) For bash and ksh users: | |
# | |
# Include the following in your ~/.bashrc or ~/.kshrc: | |
# | |
# . $HOME/.ssh/ssh-agent-setup | |
# | |
# Note, The common error is to instead include this command to ~/.bash_profile file. | |
# The authentication method of any subsequent non-login shell sessions | |
# started will not be supplied by the running ssh-agent which | |
# only calls ~/.bashrc for their shell session setup. The startup | |
# of the login shell session includes a call to ~/.bashrc anyway. | |
# | |
# | |
# | |
# 2) For csh and tcsh users: | |
# | |
# Include the following in your ~/.cshrc or ~/.tcshrc | |
# | |
# source $HOME/.ssh/ssh-agent-setup | |
# | |
# | |
SSH_ENV=$HOME/.ssh/env-$HOSTNAME | |
SSH_CONFIG=$HOME/.ssh/config | |
function ssh_clean { | |
d="$HOME/.ssh" | |
f0=$d/known_hosts | |
f1=$d/known_hosts_tmp | |
cat /dev/null > $f1 | |
while read host line; do | |
if [ $host != "localhost" ]; then | |
echo $host $line >> $f1 | |
mv $f1 $f0 | |
chmod 600 $f0 | |
} | |
# Initialize new agent and add authentication | |
function start_agent { | |
echo "Initialising new SSH agent..." | |
# Start authenticating daemon | |
# No authentications set up yet, just starting daemon! | |
ssh-agent | head -2 > ${SSH_ENV} | |
chmod 600 ${SSH_ENV} | |
# Find SSH_AUTH_SOCK and SSH_AGENT_PID of the available daemon | |
. ${SSH_ENV} > /dev/null | |
# Add authentication to this and only this daemon | |
ssh-add | |
} | |
# Setup SSH Agent forwarding | |
function ssh_enable_forward { | |
grep -q ^ForwardAgent $SSH_CONFIG &> /dev/null | |
local forward_enabled=$? | |
if [[ ! -f $SSH_CONFIG || $forward_enabled -ne 0 ]]; then | |
echo "Enabling SSH Agent Forwarding..." | |
cp $SSH_CONFIG ${SSH_CONFIG}_save | |
echo "ForwardAgent yes" > $SSH_CONFIG | |
cat ${SSH_CONFIG}_save >> $SSH_CONFIG | |
chmod 600 ${SSH_CONFIG}_save | |
fi | |
} | |
if [ -f "$SSH_ENV" ]; then | |
# Find SSH_AUTH_SOCK and SSH_AGENT_PID of the available daemon | |
. ${SSH_ENV} > /dev/null | |
# Check if the agent is still running | |
ierr=0 | |
ps ${SSH_AGENT_PID} > /dev/null || ierr=1 | |
if [ $ierr == "0" ]; then | |
echo > /dev/null | |
else | |
# If not initialize new agent and | |
# add authentication | |
start_agent; | |
fi | |
else | |
start_agent; | |
fi | |
# Clean localhost entry in the known host file | |
#ssh_clean | |
# Enable forwarding | |
ssh_enable_forward |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment