forked from ChrisKaufmann/totools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
to
executable file
·68 lines (60 loc) · 1.64 KB
/
to
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env bash
#this will copy our public key to a remote host and ssh to it.
if [[ $0 =~ "tox" ]]
then
x=' -X '
fi
userhost=$1
keyfile=~/.ssh/id_rsa.pub
authkeyfile='~/.ssh/authorized_keys'
#if no userhost is specified, print usage and exit
if [ ! $userhost ]
then
echo "usage: $0 [user@]hostname[:port] <defaults to root@>"
exit
fi
#if no ssh public key exists, create one in the default spot
if [ ! -e $keyfile ]
then
echo "Error, no public key file,please run ssh-keygen"
exit
fi
#now get the key itself into a variable
mypubkey=`cat $keyfile`
#if no username is passed (like someuser@somehost), use root by default
if [[ ! "$userhost" =~ '@' ]]
then
userhost=root@$1
fi
#see if there's a port on the end, if so create $p with the port
if [[ $userhost =~ (.*@.*):(.*) ]]
then
userhost="${BASH_REMATCH[1]}"
port=" -p ${BASH_REMATCH[2]} "
else
port=" "
fi
#this keeps it to one time needed to enter the password,
#it'll create the .ssh directory with right perms, touch the key file,
#create a backup without our key (no dupes),
#and copy it back
ssh $port $userhost "mkdir -p .ssh;
chmod 700 .ssh;
touch $authkeyfile;
cp $authkeyfile ${authkeyfile}.bak;
grep -v '$mypubkey' ${authkeyfile}.bak > $authkeyfile;
echo '$mypubkey' >> $authkeyfile"
#if no username is passed (like someuser@somehost), use root by default
if [[ "$userhost" == 'root@'$1 ]]
then
userhost=root@$1
for f in /usr/bin/ami /usr/bin/didi /usr/bin/killthe /usr/bin/redo /usr/bin/ducks
do
if [ -e $f ]
then
scp $port -q $f $userhost:$f
fi
done
fi
#and finally, ssh to the host.
ssh $x $port $userhost $2