From 847943f807fd4dab702b26929ec9ec8c3992ac1f Mon Sep 17 00:00:00 2001 From: Kevin Cotugno Date: Thu, 19 Oct 2017 22:55:20 -0700 Subject: [PATCH] Only use the sentinal if it exists --- profile | 6 ++++-- ssh-sentinel.sh | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 ssh-sentinel.sh diff --git a/profile b/profile index 6045384..4488487 100644 --- a/profile +++ b/profile @@ -48,10 +48,12 @@ function passgen () { tr "$tr1" "$tr2" && echo } +if [[ -f "$HOME/.ssh-sentinel.sh" ]]; then + source "$HOME/.ssh-sentinel.sh" +fi + export EDITOR='nvim' alias l="ls -lah" alias ll="ls -lh" alias la="ls -lah" - -source $HOME/.ssh-sentinel.sh diff --git a/ssh-sentinel.sh b/ssh-sentinel.sh new file mode 100644 index 0000000..5e57c4f --- /dev/null +++ b/ssh-sentinel.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash + +file="$HOME/.ssh-agent" +found=0 + +which ssh-agent &> /dev/null +if (( $? )); then + exit 1 +fi + +if [[ -e $file ]]; then + sockenv=`cat $file | grep SSH_AUTH_SOCK` + pidenv=`cat $file | grep SSH_AGENT_PID` + sock=`echo "$sockenv" | grep -oP "(?<=\=)/.*$"` + pid=`echo "$pidenv" | grep -o "[0-9]*"` + proc=`ps ax | grep "^ $pid.*ssh-agent"` + + if [[ -S "$sock" && -O "$sock" && -n "$proc" ]]; then + export "$sockenv" + export "$pidenv" + found=1 + fi +fi + +if (( ! $found )); then + export `ssh-agent -s | grep -oP "^.+(?=; export)" | tee "$file"` +fi + +chmod 600 "$file"