Last active
April 7, 2022 03:47
-
-
Save kou1okada/4d9d049d5b75094d6babd41258a8ba17 to your computer and use it in GitHub Desktop.
attach-ssh-agent.sh - Attach ssh-agent
This file contains hidden or 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
#!/usr/bin/env bash | |
# | |
# attach-ssh-agent.sh - Attach ssh-agent | |
# Copyright (c) 2020 Koichi OKADA. All rights reserved. | |
# This script is distributed under the MIT License. | |
# | |
# Usage: | |
# Include from .bashrc as: | |
# . attach-ssh-agent.sh | |
# | |
#cat <<EOD >/dev/null | |
function is_alive_ssh_agent () | |
{ | |
[ -S "$SSH_AUTH_SOCK" ] && echo|nc -w 1 -U "$SSH_AUTH_SOCK" | |
} | |
function attach_ssh_agent () | |
{ | |
[ -n "$SSH_CLIENT" ] && return | |
is_alive_ssh_agent && return | |
[ -e ~/.ssh/.ssh-agent.bash ] && . ~/.ssh/.ssh-agent.bash | |
is_alive_ssh_agent && return | |
( killall -u $USER ssh-agent & ) >&/dev/null | |
[ -d ~/.ssh ] || { | |
mkdir -p ~/.ssh && chmod 700 ~/.ssh || { | |
echo -e "${SGR_error}Error:${SGR_rest} ~/.ssh could not be configured." | |
ls -ld ~/.ssh | |
return 1 | |
} | |
} | |
ssh-agent | grep -Ev '^echo ' > ~/.ssh/.ssh-agent.bash | |
[ -e ~/.ssh/.ssh-agent.bash ] && . ~/.ssh/.ssh-agent.bash | |
is_alive_ssh_agent && return | |
local SGR_reset='\e[0m' | |
local SGR_error='\e[31;1m' | |
echo -e "${SGR_error}Error:${SGR_rest} Couldn't attach ssh-agent." | |
return 1 | |
} | |
attach_ssh_agent | |
#EOD |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment