-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcopykey
More file actions
executable file
·148 lines (123 loc) · 2.69 KB
/
copykey
File metadata and controls
executable file
·148 lines (123 loc) · 2.69 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/bin/bash
#
# copykey
#
[ -n "$MYDIR" ] || {
declare MYDIR=
MYDIR="$(dirname "$(readlink -f "$0")")"
}
declare remote_user
declare remote_host
declare -a argv=()
declare -i argc=2
declare usagestr="$(
cat <<EOF
$(basename "$0") remote_user remote_host
Arguments
---------
remote_user : username on the remote host
remote_host : ip of the remote host
\0
EOF
)"
usage() {
echo -e "$usagestr"
}
# control_c: run if user hits control-c
#
# Global
# CTLC_EXIT - bash environment variable
#
control_c() {
echo -e "\nCtrl-c detected\nCleaning up and exiting."
exit $CTLC_EXIT
}
# exitme
#
# Arguments
# $1 - exit code
# $2 - optional message
#
exitme() {
local -i code="$1"
local msg="$2"
((code == 0)) && exit "$code"
echo -e "$msg"
usage
exit "$code"
}
# check_arg
#
# Globals:
# argv - array containing command line arguments
#
check_arg() {
local arg="$1"
# If 1st char is a hyphen, then invalid switch
[ "${arg:0:1}" == "-" ] && return 1
# Otherwise, add the arg to the argary
argv+=("$arg")
return 0
}
# parseopts
#
# Globals
# argv
# argc
#
parseopts() {
while (($# > 0)); do
case "$1" in
-h | --help )
echo -e "$usagestr"
exit 0
;;
* ) argv+=("$1")
;;
esac
shift
done
}
main() {
# Trap for control-c
trap control_c SIGINT
parseopts "$@"
remote_user="${argv[0]}"
remote_host="${argv[1]}"
### Detailed Explanation:
# `LOCAL_HOST=$(hostname)`: This captures the hostname of your local machine.
# `localkey=$(cat ~/.ssh/id_rsa.pub)`: This captures the content of your public key.
# `remote_script`: This is a here document that contains the script to
# be run on the remote host.
#
# - Ensures the `.ssh` directory exists.
# - Appends the local public key to the `authorized_keys` file.
#
# - Uses `ssh-keyscan` to add the local host's public key to the
# `known_hosts` file on the remote host.
#
# - Sets the correct permissions for the `authorized_keys` file and the
# `.ssh` directory.
#
# `ssh -o "StrictHostKeyChecking=no" $remote_user@$remote_host "$remote_script"`
# - connects to the remote host and runs the script.
### Script to Automate the Process
key=${3:-~/.ssh/id_rsa.pub}
if [ -z "$remote_user" ] || [ -z "$remote_host" ]; then
echo "Usage: $0 <remote_user> <remote_host> [ssh_key_path]"
exit 1
fi
LOCAL_HOST=$(hostname)
localkey=$(cat "$key")
remote_script=$(cat <<EOF
mkdir -p ~/.ssh
echo "$localkey" >> ~/.ssh/authorized_keys
ssh-keyscan -H $LOCAL_HOST >> ~/.ssh/known_hosts
chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh
EOF
)
ssh -o "StrictHostKeyChecking=no" "$remote_user@$remote_host" "$remote_script"
exitme 0
}
main "$@"