-
Notifications
You must be signed in to change notification settings - Fork 0
/
handshaker.sh
88 lines (78 loc) · 2.45 KB
/
handshaker.sh
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
#!/bin/bash
unset knows_hosts_file
unset hosts
############################################################
# source check
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]
then
echo "[INFO]: script ${BASH_SOURCE[0]} is being sourced ..."
else
echo "[ERROR]: > You have to run the script as source: 'source ${BASH_SOURCE[0]} 5.7.1'"
return
fi
############################################################
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
echo "
######################################################################################
${BASH_SOURCE[0]} - certificates handler
######################################################################################
HELP:
[ -h/--help ]: outputs this helper.
[ -H/--hosts_file ]: ~/.ssh/known_hosts
[ arg* ]: list of hostnames: 10.10.10.1:2022 192.168.0.1 127.0.0.1:10023
EXAMPLE:
You can run the script as:
'source ${BASH_SOURCE[0]} -H ~/.ssh/known_hosts 127.0.0.1:10022 127.0.0.1:10023'
######################################################################################
"
return
;;
-H|--hosts_file)
knows_hosts_file="$2"
shift
shift
;;
-*|--*)
echo "ERROR: unknown argument $1"
return
;;
*)
hosts+=("$1")
shift
;;
esac
done
# echo ${#hosts[@]}
echo "[INFO]: knows_hosts_file at: $knows_hosts_file"
if [[ -f "${knows_hosts_file}" ]]; then
echo "[INFO]: known_hosts file was found, appending fingerprints at location..."
else
full_path=(${knows_hosts_file//[\/]/ })
dir_path=""
known_hosts_file="known_hosts"
for ((i=0; i<${#full_path[@]}; i+=1)); do
if [ $i -lt $(( ${#full_path[*]} - 1 )) ]; then
dir_path="${dir_path}/${full_path[$i]}"
else
known_hosts_file="${full_path[$i]}"
fi
done
echo "[INFO]: creating known_hosts at selected location: '${dir_path}/${known_hosts_file}'"
mkdir -m 700 -p ${dir_path}
chmod 644 "${dir_path}/${known_hosts_file}">>"${dir_path}/${known_hosts_file}"
fi
############################################################
for host in "${hosts[@]}"; do
echo "[INFO]: adding host: $host to known_hosts file"
host=(${host//[:]/ })
if [ ${#host[@]} -eq 1 ]; then
ssh-keyscan ${host} >> ${knows_hosts_file}
else
ssh-keyscan -p ${host[1]} ${host[0]} >> ${knows_hosts_file}
fi
done
# generate certs, then ssh-copy-id
unset knows_hosts_file
unset hosts