From a727341b56f18f227c244b53e1c392774367ea4b Mon Sep 17 00:00:00 2001 From: Kevin Cotugno Date: Wed, 21 Nov 2018 16:51:14 -0800 Subject: [PATCH] Fix ssh-sentinel --- .ssh-sentinel | 26 ++++++++++++++++++++++++++ .zshrc | 2 ++ install.zsh | 2 ++ ssh-sentinel.sh | 29 ----------------------------- 4 files changed, 30 insertions(+), 29 deletions(-) create mode 100644 .ssh-sentinel delete mode 100644 ssh-sentinel.sh diff --git a/.ssh-sentinel b/.ssh-sentinel new file mode 100644 index 0000000..21126c7 --- /dev/null +++ b/.ssh-sentinel @@ -0,0 +1,26 @@ +file="$HOME/.ssh-agent" + +which ssh-agent &> /dev/null +if (( $? )); then + exit 1 +fi + +if [[ -n "$SSH_AUTH_SOCK" ]] && [[ -n "$SSH_AGENT_PID" ]]; then + return +fi + +if [[ -e $file ]]; then + sockenv=`cat $file | grep SSH_AUTH_SOCK` + pidenv=`cat $file | grep SSH_AGENT_PID` + pid=`printf "$pidenv" | grep -o "[0-9]*"` + + ps --pid "$pid" &> /dev/null + if (( ! $? )); then + export "$sockenv" + export "$pidenv" + return + fi +fi + +export `ssh-agent -s | grep -oP "^.+(?=; export)" | tee "$file"` +chmod 600 "$file" diff --git a/.zshrc b/.zshrc index d004747..dc7eab0 100644 --- a/.zshrc +++ b/.zshrc @@ -1,4 +1,5 @@ local oh_my_zsh="$HOME/.oh-my-zsh" +source $HOME/.ssh-sentinel if [[ -d "$oh_my_zsh" ]]; then local theme="jispwoso" @@ -48,6 +49,7 @@ else export EDITOR="vi" fi + function passgen () { local len="$1" local lower="$2" diff --git a/install.zsh b/install.zsh index bb950df..ed1f882 100755 --- a/install.zsh +++ b/install.zsh @@ -26,6 +26,7 @@ function uninstall { eval $rm "$nvim_dir/autoload" eval $rm "$nvim_dir/init.vim" eval $rm "$HOME/.Xresources" + eval $rm "$HOME/.ssh-sentinel" } function install { @@ -35,6 +36,7 @@ function install { eval $link "$PWD/.emacs.d/themes/" "$emacs_dir/themes" eval $link "$PWD/.zshrc" "$HOME/.zshrc" eval $link "$PWD/.tmux.conf" "$HOME/.tmux.conf" + eval $link "$PWD/.ssh-sentinel" "$HOME/.ssh-sentinel" eval $link "$PWD/.config/nvim/autoload" "$nvim_dir/autoload" eval $link "$PWD/.config/nvim/init.vim" "$nvim_dir/init.vim" diff --git a/ssh-sentinel.sh b/ssh-sentinel.sh deleted file mode 100644 index 5e57c4f..0000000 --- a/ssh-sentinel.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/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"