-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.ssh_agent
More file actions
39 lines (33 loc) · 1.6 KB
/
.ssh_agent
File metadata and controls
39 lines (33 loc) · 1.6 KB
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
#!/bin/bash
# Get fingerprints of all keys currently loaded in the ssh-agent
loaded_keys=$(ssh-add -l | awk '{print $2}')
# Export loaded_keys for access within subshell invoked by find ... -exec
export loaded_keys
if [ -z "$loaded_keys" ]; then
echo "No SSH keys are currently loaded in the ssh-agent."
# If you want to proceed to check and potentially load keys, don't exit here
fi
echo "Comparing loaded SSH keys with keys in ~/.ssh..."
# Process each public key in the ~/.ssh directory
find ~/.ssh -name "*.pub" -exec bash -c '
for pub_key_path; do
# Extract the fingerprint of the current public key, considering removal of the MD5 prefix for consistency
fingerprint=$(ssh-keygen -l -f "$pub_key_path" | awk "{print \$2}" | sed -e "s/MD5://")
# Use the exported loaded_keys within this subshell context
if echo "${loaded_keys}" | grep -q "$fingerprint"; then
echo "Public key $pub_key_path is already loaded."
else
echo "Public key $pub_key_path is NOT loaded. Attempting to load corresponding private key..."
private_key_path="${pub_key_path%.*}"
if [ -f "$private_key_path" ]; then
if ssh-add "$private_key_path" &> /dev/null; then
echo "Loaded $private_key_path into ssh-agent."
else
echo "Failed to load $private_key_path. Check if ssh-agent is running and if the key is password protected."
fi
else
echo "Corresponding private key $private_key_path not found."
fi
fi
done
' bash {} +