sshroot: better handling of getting keys
[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=900
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 --eval --host "$hostopt-root" $keys)
15     exec "$@"
16 }
17
18 function with-agent
19 {
20     unset SSH_AUTH_SOCK SSH_AGENT_PID
21     eval $(keychain --eval --host "$hostopt-root")
22     exec "$@"
23 }
24
25 command="$1"
26 shift
27
28 case "$command" in
29     init)
30         echo "Loading default keys (lifetime $lifetime)..."
31         with-agent ssh-add -t $lifetime "$@" $keys
32         ;;
33     add)
34         echo "Loading keys (lifetime $lifetime):" "$@"
35         with-agent ssh-add -t $lifetime "$@"
36         ;;
37     list)
38         with-agent ssh-add -l
39         ;;
40     destroy)
41         with-agent ssh-add -D
42         ;;
43     shell)
44         with-keys $SHELL
45         ;;
46     ssh)
47         with-keys ssh -l root "$@"
48         ;;
49     *)
50         if [ -z "$command" ]; then
51             echo "Usage: $0 init" >&2
52             echo "       $0 add" >&2
53             echo "       $0 list" >&2
54             echo "       $0 destroy" >&2
55             echo "       $0 shell" >&2
56             echo "       $0 ssh [args]" >&2
57             echo "       $0 [cmd]" >&2
58         else
59             echo Executing: "$command" "$@"
60             with-agent "$command" "$@"
61         fi
62         ;;
63 esac