Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

auto ssh agent / key adding for linux

  • Loading branch information...
commit ee5173247dbe138818e3e2efae02de72525463ca 1 parent 86d67c1
@marshall authored
Showing with 28 additions and 0 deletions.
  1. +28 −0 ssh/ssh.zsh
View
28 ssh/ssh.zsh
@@ -0,0 +1,28 @@
+setup_ssh_agent() {
+ # Try to find an existing ssh-agent before starting a new one
+ agent_pid=$(ps -ef | grep "ssh-agent" | grep -v "grep" | awk '{print $2}')
+
+ if [[ -z $agent_pid ]]; then
+ # None exists, start ssh-agent
+ eval "$(ssh-agent)"
+ else
+ agent_ppid=$(ps -ef | grep "ssh-agent" | grep -v "grep" | awk '{print $3}')
+ agent_sock=$(find /tmp -path "*ssh*" -type s -iname "agent.$agent_ppid" 2>/dev/null)
+ echo "[ssh-agent] PID: $agent_pid, Sock: $agent_sock"
+ export SSH_AGENT_PID="$agent_pid"
+ export SSH_AUTH_SOCK="$agent_sock"
+ fi
+
+ known_keys=$(ssh-add -l | awk '{print $3}')
+ cat ~/.ssh/config | grep IdentityFile | awk '{print $2}' | while read key; do
+ # Only ssh-add unknown identity files
+ (echo $known_keys | grep $key > /dev/null) && continue
+
+ echo "$fg[white]Adding ssh key $reset_color$fg[blue]$key$reset_color"
+ ssh-add $key
+ done
+}
+
+if [[ "$OS_NAME" = "Linux" ]]; then
+ setup_ssh_agent
+fi
Please sign in to comment.
Something went wrong with that request. Please try again.