forked from Schaffenburg/schaffendoor-sshkeys
33 lines
1,010 B
Bash
Executable file
33 lines
1,010 B
Bash
Executable file
#!/bin/bash
|
|
|
|
# Input file containing the SSH public keys
|
|
key_file=$1
|
|
|
|
# Read the file line by line
|
|
while IFS= read -r line; do
|
|
# Skip empty lines
|
|
if [ -z "$line" ]; then
|
|
continue
|
|
fi
|
|
|
|
# Trim leading/trailing whitespace (preserving the key and comment structure)
|
|
trimmed_line=$(echo "$line" | xargs)
|
|
|
|
# Validate the SSH key format:
|
|
# Starts with ssh-<type>, followed by base64-encoded data, and an optional comment
|
|
echo $key_file
|
|
if ! echo "$trimmed_line" | grep -Eq '^ssh-(rsa|ed25519|dss|ecdsa) [A-Za-z0-9+/=]+ ?.*$'; then
|
|
echo "Invalid key format: $line"
|
|
exit 1
|
|
fi
|
|
|
|
# Ensure there is only one key on the line: a valid key should only have one space separating key data from the comment
|
|
space_count=$(echo "$trimmed_line" | grep -o ' ' | wc -l)
|
|
if [ "$space_count" -gt 2 ]; then
|
|
echo "Invalid: Multiple keys found on the same line"
|
|
exit 1
|
|
fi
|
|
|
|
done < "$key_file"
|
|
|
|
echo "All keys are valid and correctly formatted."
|