-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_local_ssh.sh
More file actions
executable file
·105 lines (87 loc) · 2.22 KB
/
setup_local_ssh.sh
File metadata and controls
executable file
·105 lines (87 loc) · 2.22 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/bash
#
# Script to setup local ssh keys
# Allows ssh to localhost without password
#
# by Craig Miller 24 February 2010
#
function usage {
echo " $0 - Used to setup local SSH Key (e.g. ssh to \
localhost without a password challenge) "
echo " e.g. $0"
echo " "
echo " By Craig Miller - Version: $VERSION"
exit 1
}
# Script Defaults
numopts=0
VERSION=0.96
SSH_HOME=$HOME/.ssh
while getopts "vV" options; do
case $options in
V ) show_version=TRUE
let numopts+=1;;
v ) show_version=TRUE
let numopts+=1;;
h ) usage;;
\? ) usage # show usage with flag and no value
exit 1;;
* ) usage # show usage with unknown flag
exit 1;;
esac
done
# remove the options as cli arguments
shift $numopts
# check that there are no arguments left to process
if [ $# -ne 0 ]; then
usage
exit 1
fi
if [ $show_version ]; then
usage
exit 1
fi
#======== Actual work performed by script ============
#check if ssh is installed
if [ ! -x /usr/bin/ssh-keygen ]; then
echo "==================="
echo "ACK! ssh-keygen NOT installed!"
echo "Please install ssh package before running $0"
echo "==================="
exit 1
fi
#check if keys already exist
if [ ! -e $SSH_HOME/id_rsa.pub ]; then
mkdir -p $HOME/.ssh
echo "==================="
echo "Generating ssh keys"
echo "==================="
ssh-keygen -t rsa -N "" -f $SSH_HOME/id_rsa
fi
#check authorized_keys2
RESULT=""
if [ -e $SSH_HOME/authorized_keys2 ]; then
TEMP=`cat $SSH_HOME/id_rsa.pub`
RESULT=`grep "$TEMP" $SSH_HOME/authorized_keys2`
fi
#create authorized key
if [ "$RESULT" == "" ]; then
echo "======================="
echo "Creating Authorized Key"
echo "======================="
cd $SSH_HOME
cat id_rsa.pub >> authorized_keys2
#return to previous directory
cd -
fi
echo "==============="
echo "Testing SSH Key"
echo "==============="
echo "ssh localhost"
# this also adds localhost to known_hosts file
ssh -o StrictHostKeyChecking=no localhost 'hostname; exit'
ssh -o StrictHostKeyChecking=no $HOSTNAME 'hostname; exit'
# Pau!
echo "======================================="
echo "Setup of SSH Key on localhost Complete!"
echo "======================================="