Last active
January 17, 2020 06:29
-
-
Save redmcg/571bc4f1f67aeb1cf016af278205c12f to your computer and use it in GitHub Desktop.
An awk script that provides the same functionality as ssh-keygen -F <host>
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 | |
host=$1 | |
awk -v host=$host ' | |
/^|1/ { | |
split($1, a, /\|/) | |
key=a[3] | |
hmac=a[4] | |
"echo -n " host " | openssl sha1 -mac HMAC -macopt hexkey:$(echo " key " | base64 -d | xxd -p) -binary | base64" | getline myhmac | |
if (hmac == myhmac) { | |
print "# Host " host " found: line " NR | |
} | |
}' ~/.ssh/known_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 | |
host=$1 | |
awk -v host=$host ' | |
function pad_key(pad, i, val) { | |
for (i = 0; i < 64; i++) { | |
val = sprintf("%s%02x", val, xor(strtonum("0x" substr(hexkey, i*2+1, 2)), pad)) | |
} | |
return val | |
} | |
/^|1/ { | |
split($1, a, /\|/) | |
key=a[3] | |
hmac=a[4] | |
"echo " key " | base64 -d | xxd -p " | getline hexkey | |
o_key_pad = pad_key(0x5c) | |
i_key_pad = pad_key(0x36) | |
"bash -c '\''cat <(echo -n " o_key_pad " | xxd -r -p) <(cat <(echo -n " i_key_pad " | xxd -r -p) <(echo -n " host ") | sha1sum | xxd -r -p) | sha1sum | xxd -r -p | base64'\''" | getline myhmac | |
if (hmac == myhmac) { | |
print "# Host " host " found: line " NR | |
} | |
}' ~/.ssh/known_hosts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment