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