use with caution! This script will copy your pub key to a remote host so you can login or run remote scripts without a password. IMPORTANT: if you need to run a single script on the remote host, it would be better to authorize a key for just that script.
1
2
3
4 function usage()
5 {
6 echo ""
7 echo "Authorizes a host for automatic SSH use by sending your key to the remote host ..."
8 echo "Usage: $0 remote_host_to_authorize [username:=defaults to current username]"
9 echo ""
10 }
11
12 function cleanup()
13 {
14 if [ -f $TEMP_PUB_KEY_XFER ]
15 then
16 rm $TEMP_PUB_KEY_XFER
17 fi
18 }
19
20 function exit_on_error()
21 {
22 cleanup
23 exit 1
24 }
25
26 if [ $
27 then
28 usage
29 exit 0
30 fi
31
32 PUB_KEY=~/.ssh/id_dsa.pub
33 if [ $
34 USER=$2
35 else
36 USER=`whoami`
37 fi
38 HOST_TO_AUTH=$1
39 TEMP_PUB_KEY_XFER=/tmp/$USER"_TEMP_KEY"
40
41 echo "checking for $PUB_KEY ..."
42 if [ ! -f $PUB_KEY ]; then
43 echo "generating your dsa public key (leave passphrase blank and save to $PUB_KEY when prompted) ..."
44 ssh-keygen -t dsa
45 if [ $? -ne 0 ]; then
46 echo "ssh-keygen failed"
47 exit_on_error
48 fi
49 fi
50 echo "OK"
51
52 echo "for the following commands you will be asked to supply your password for $HOST_TO_AUTH :"
53
54 echo "copying a temp pub key to $HOST_TO_AUTH ..."
55 cat $PUB_KEY > $TEMP_PUB_KEY_XFER
56 chmod 700 $TEMP_PUB_KEY_XFER
57 echo "OK"
58
59 remote_key=`basename $TEMP_PUB_KEY_XFER`
60 scp $TEMP_PUB_KEY_XFER $USER@$HOST_TO_AUTH:~/$remote_key
61 if [ $? -ne 0 ]; then
62 echo "scp failed"
63 exit_on_error
64 fi
65
66 echo "authorizing $HOST_TO_AUTH for automatic SSH use ..."
67 ssh $USER@$HOST_TO_AUTH "cat ~/$remote_key >> ~/.ssh/authorized_keys; rm ~/$remote_key"
68 if [ $? -ne 0 ]; then
69 echo "ssh failed"
70 exit_on_error
71 fi
72 echo "OK"
73
74 cleanup
75 echo "authorization successful! you can now login automatically to $HOST_TO_AUTH"
76 exit 0