Retry known hosts validation
@@ -6,20 +6,6 @@ known_hosts_file=${1}
known_hosts_file=${known_hosts_file:-/etc/ssh/ssh_known_hosts}
hosts="github.com gitlab.com bitbucket.org ssh.dev.azure.com vs-ssh.visualstudio.com"
-retries=10
-count=0
-ok=false
-wait=2
-until ${ok}; do
- ssh-keyscan ${hosts} > ${known_hosts_file} && ok=true || ok=false
- sleep ${wait}
- count=$(($count + 1))
- if [[ ${count} -eq ${retries} ]]; then
- echo "ssh-keyscan failed, no more retries left"
- exit 1
- fi
-done
-
# The heredoc below was generated by constructing a known_hosts using
#
# ssh-keyscan github.com gitlab.com bitbucket.org ssh.dev.azure.com vs-ssh.visualstudio.com > ./known_hosts
@@ -43,6 +29,12 @@ trap cleanup EXIT
# make sure sorting is in the same locale as the heredoc
export LC_ALL=C
+
+generate() {
+ ssh-keyscan ${hosts} > ${known_hosts_file}
+}
+
+validate() {
ssh-keygen -l -f ${known_hosts_file} | sort > "$fingerprints"
diff - "$fingerprints" <<EOF
@@ -54,3 +46,19 @@ diff - "$fingerprints" <<EOF
256 SHA256:HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSnUw gitlab.com (ECDSA)
256 SHA256:eUXGGm1YGsMAS7vkcx6JOJdOGHPem5gQp4taiCfCLB8 gitlab.com (ED25519)
EOF
+
+}
+
+retries=10
+count=0
+ok=false
+wait=2
+until ${ok}; do
+ generate && validate && ok=true || ok=false
+ sleep ${wait}
+ count=$(($count + 1))
+ if [[ ${count} -eq ${retries} ]]; then
+ echo "ssh-keyscan failed, no more retries left"
+ exit 1
+ fi
+done
