sshroot: better handling of getting keys
[user/alex/software/my-snippets.git] / sshroot
diff --git a/sshroot b/sshroot
index bac553444711b88ae85fc6c8b4e9722230c260cf..6a0c4b5f6e8d906e1d572c75e54a7e3a2b78adf5 100755 (executable)
--- a/sshroot
+++ b/sshroot
@@ -9,6 +9,13 @@ hostopt=""
 keys=~/.ssh/id_rsa_root
 
 function with-keys
+{
+    unset SSH_AUTH_SOCK SSH_AGENT_PID
+    eval $(keychain --eval --host "$hostopt-root" $keys)
+    exec "$@"
+}
+
+function with-agent
 {
     unset SSH_AUTH_SOCK SSH_AGENT_PID
     eval $(keychain --eval --host "$hostopt-root")
@@ -21,17 +28,17 @@ shift
 case "$command" in
     init)
         echo "Loading default keys (lifetime $lifetime)..."
-        with-keys ssh-add -t $lifetime "$@" $keys
+        with-agent ssh-add -t $lifetime "$@" $keys
         ;;
     add)
         echo "Loading keys (lifetime $lifetime):" "$@"
-        with-keys ssh-add -t $lifetime "$@"
+        with-agent ssh-add -t $lifetime "$@"
         ;;
     list)
-        with-keys ssh-add -l
+        with-agent ssh-add -l
         ;;
     destroy)
-        with-keys ssh-add -D
+        with-agent ssh-add -D
         ;;
     shell)
         with-keys $SHELL
@@ -50,7 +57,7 @@ case "$command" in
             echo "       $0 [cmd]" >&2
         else
             echo Executing: "$command" "$@"
-            with-keys "$command" "$@"
+            with-agent "$command" "$@"
         fi
         ;;
 esac