Skip to content

Instantly share code, notes, and snippets.

@mckern
Last active August 29, 2015 14:10
Show Gist options
  • Save mckern/c495c06c9530741a928d to your computer and use it in GitHub Desktop.
Save mckern/c495c06c9530741a928d to your computer and use it in GitHub Desktop.
Initializing SSH agent settings
#!/bin/bash
function test_socket {
if [[ ${SSH_AUTH_SOCK+_} && -S ${SSH_AUTH_SOCK} ]]; then
: "Socket found"
return 0
fi
: "No socket found"
return 1
}
function initialize_socket {
local ssh_conf_dir="${SSH_CONF_DIR:-${HOME}/.ssh}"
local ssh_agent_conf="${SSH_AGENT_CONF:-${ssh_conf_dir}/agent}"
local ssh_agent="${SSH_AGENT:-$(which ssh-agent 2>/dev/null)}"
# Ensure that an SSH agent command exists
if [[ -z ${ssh_agent} ]]; then
: "No SSH agent found"
return 1
fi
# Ensure that the ssh config directory exists
if [[ ! -d ${ssh_conf_dir} ]]; then
: "No SSH configuration directory (${ssh_conf_dir}) exists"
return 1
fi
# Initialize the socket since nothing has been found
"${ssh_agent}" -s | head -n2 > "${ssh_agent_conf}"
[[ $? ]] && source "${ssh_agent_conf}"
return $?
}
# See if there's a socket, and initialize a socket if there's not
test_socket || initialize_socket
unset initialize_socket
unset test_socket
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment