sshroot: minor cleanup, feature, and bugfixes
[user/alex/software/my-snippets.git] / sshroot
1 #!/bin/bash
2
3 # lifetime, in minutes
4 lifetime=15
5
6 hostopt=""
7 [ -z "$hostopt" ] && hostopt="${HOSTNAME}"
8 [ -z "$hostopt" ] && hostopt=`uname -n 2>/dev/null || echo unknown`
9 keys=~/.ssh/id_rsa_root
10
11 function with-keys
12 {
13     unset SSH_AUTH_SOCK SSH_AGENT_PID
14     eval $(keychain --timeout $lifetime --eval --host "$hostopt-root" $keys)
15     export SSHROOT=1
16     exec "$@"
17 }
18
19 function with-agent
20 {
21     unset SSH_AUTH_SOCK SSH_AGENT_PID
22     eval $(keychain --timeout $lifetime --eval --host "$hostopt-root")
23     export SSHROOT=1
24     echo with-agent: Running: "$@"
25     exec "$@"
26 }
27
28 command="$1"
29 shift
30
31 case "$command" in
32     init)
33         echo "Loading default keys (lifetime $lifetime)..."
34         with-agent ssh-add -t ${lifetime}m "$@" $keys
35         ;;
36     add)
37         echo "Loading keys (lifetime $lifetime):" "$@"
38         with-agent ssh-add -t ${lifetime}m "$@"
39         ;;
40     list)
41         with-agent ssh-add -l
42         ;;
43     destroy)
44         with-agent ssh-add -D
45         ;;
46     shell)
47         with-keys "$SHELL" "$@"
48         ;;
49     ssh)
50         #with-keys ssh -l root "$@"
51         with-keys ssh "$@"
52         ;;
53     *)
54         if [ -z "$command" ]; then
55             echo "Usage: $0 init" >&2
56             echo "       $0 add" >&2
57             echo "       $0 list" >&2
58             echo "       $0 destroy" >&2
59             echo "       $0 shell" >&2
60             echo "       $0 ssh [args]" >&2
61             echo "       $0 [cmd]" >&2
62         else
63             echo Executing: "$command" "$@"
64             with-agent "$command" "$@"
65         fi
66         ;;
67 esac