Add various sshroot options
authorAlex Dehnert <adehnert@mit.edu>
Sat, 15 Dec 2012 10:02:34 +0000 (05:02 -0500)
committerAlex Dehnert <adehnert@mit.edu>
Sat, 15 Dec 2012 10:02:34 +0000 (05:02 -0500)
-l  key lifetime (default: 15 minutes)
-k  key suffix to use (default: "root")
-H  hostname to use (default: local hostname, as derived by various checks)

sshroot

diff --git a/sshroot b/sshroot
index 5c41dfab1cdeab2192164883f57693a652b291c5..f4d8996ffc2044745e1ae8d0553715657c6c9a13 100755 (executable)
--- a/sshroot
+++ b/sshroot
@@ -2,16 +2,29 @@
 
 # lifetime, in minutes
 lifetime=15
+keysuffix=root
 
 hostopt=""
 [ -z "$hostopt" ] && hostopt="${HOSTNAME}"
 [ -z "$hostopt" ] && hostopt=`uname -n 2>/dev/null || echo unknown`
-keys=~/.ssh/id_rsa_root
+
+while getopts "H:k:l:" opt; do
+    case "$opt" in
+    H)  hostopt="$OPTARG";;
+    k)  keysuffix="$OPTARG";;
+    l)  lifetime="$OPTARG";;
+    \?) die "$0 [-H hostname] [-k keysuffix] [-l lifetime]"
+    esac
+done
+shift $(($OPTIND - 1))
+
+keys="$HOME/.ssh/id_rsa_$keysuffix"
+keychain_host="$hostopt-$keysuffix"
 
 function with-keys
 {
     unset SSH_AUTH_SOCK SSH_AGENT_PID
-    eval $(keychain --timeout $lifetime --eval --host "$hostopt-root" $keys)
+    eval $(keychain --timeout "$lifetime" --eval --host "$keychain_host" $keys)
     export SSHROOT=1
     exec "$@"
 }
@@ -19,7 +32,7 @@ function with-keys
 function with-agent
 {
     unset SSH_AUTH_SOCK SSH_AGENT_PID
-    eval $(keychain --timeout $lifetime --eval --host "$hostopt-root")
+    eval $(keychain --timeout "$lifetime" --eval --host "$keychain_host")
     export SSHROOT=1
     echo with-agent: Running: "$@"
     exec "$@"
@@ -31,11 +44,11 @@ shift
 case "$command" in
     init)
         echo "Loading default keys (lifetime $lifetime)..."
-        with-agent ssh-add -t ${lifetime}m "$@" $keys
+        with-agent ssh-add -t "${lifetime}m" "$@" $keys
         ;;
     add)
         echo "Loading keys (lifetime $lifetime):" "$@"
-        with-agent ssh-add -t ${lifetime}m "$@"
+        with-agent ssh-add -t "${lifetime}m" "$@"
         ;;
     list)
         with-agent ssh-add -l