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