Last active
March 1, 2023 16:20
-
-
Save sbassett29/a83557ef2d12ab5b8edb173ff9ff6842 to your computer and use it in GitHub Desktop.
Quick way to consistently verify ssh key fingerprints of remote hosts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
################################################################################ | |
# Author: [email protected] | |
# License: Apache 2 <https://opensource.org/licenses/Apache-2.0> | |
# Description: | |
# Quick way to consistently verify ssh key fingerprints of remote hosts | |
################################################################################ | |
set -euo pipefail | |
# validate arguments | |
if ([ -z ${1+x} ]); then | |
printf "One argument required: {valid host name or ip address}. Exiting.\n" | |
exit 1 | |
fi | |
# set variables | |
# host could be ip address, host name or garbage | |
readonly host="$1" | |
# Description: Check binary dependencies | |
# Arguments: List of binaries from args | |
# Output: Error message and exit | |
function check_binaries() { | |
bins="$@" | |
for bin in "${bins[@]}"; do | |
if [[ -z $(command -v $bin) ]]; then | |
printf "Dependency '$bin' does not appear to be installed.\n" | |
printf "Exiting for now...\n" | |
exit 1 | |
fi | |
done | |
} | |
# Description: Run bin/mw_php_sec_sniff | |
# Arguments: $1 = host ip address | |
# Outputs: ssh-keygen output as text | |
function validate_host_ssh_key() { | |
local my_host="$1" | |
local keyscan_data=$(ssh-keyscan $my_host 2> /dev/null > ~/kf) | |
ssh-keygen -lf ~/kf;rm -rf ~/kf | |
} | |
# Description: Main | |
# Arguments: Arguments provided to the script | |
# Output: None | |
function main() { | |
# set bin dependencies | |
local bins=("ssh-keyscan" "ssh-keygen") | |
check_binaries "${bins[@]}" | |
validate_host_ssh_key "$host" | |
} | |
# Call main() | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment