From 2ad0fa2288b81e99ffd8679950faa4f0acfbc440 Mon Sep 17 00:00:00 2001 From: Nikolas Weger Date: Fri, 11 Dec 2020 09:55:51 +0100 Subject: [PATCH] stuff --- zsh/files/sshagent.zsh | 31 +++++++++++++++++++++++++++++++ zsh/rc.zsh | 1 + 2 files changed, 32 insertions(+) create mode 100644 zsh/files/sshagent.zsh diff --git a/zsh/files/sshagent.zsh b/zsh/files/sshagent.zsh new file mode 100644 index 0000000..e3f2e57 --- /dev/null +++ b/zsh/files/sshagent.zsh @@ -0,0 +1,31 @@ +SSH_ENV="$HOME/.ssh/environment" +KEY="$HOME/.ssh/id_rsa" + +function start_agent () { + ssh-agent -s | sed 's/^echo/#echo/' > "$SSH_ENV" + chmod 600 "$SSH_ENV" + . "$SSH_ENV" > /dev/null +} + +function add_identities () { + ssh-add -l | grep "The agent has no identities" > /dev/null + if [ $? -eq 0 ]; then + ssh-add $KEY &> /dev/null + fi +} + +function is_ssh_agent_pid_valid () { + ps -ef | grep $1 | grep -v grep | grep ssh-agent > /dev/null +} + +if [ -n "$SSH_AGENT_PID" ] && is_ssh_agent_pid_valid $SSH_AGENT_PID; then + add_identities +else + if [ -f "$SSH_ENV" ]; then + . "$SSH_ENV" > /dev/null + fi + if ! is_ssh_agent_pid_valid $SSH_AGENT_PID; then + start_agent + fi + add_identities +fi diff --git a/zsh/rc.zsh b/zsh/rc.zsh index 8acc358..4584667 100644 --- a/zsh/rc.zsh +++ b/zsh/rc.zsh @@ -45,6 +45,7 @@ setopt inc_append_history source $ZDOTDIR/files/dircolor.zsh source $ZDOTDIR/files/aliases.zsh source $ZDOTDIR/files/direnv.zsh +source $ZDOTDIR/files/sshagent.zsh zinit snippet OMZL::history.zsh zinit snippet OMZL::functions.zsh zinit snippet OMZL::completion.zsh