Skip to content

Instantly share code, notes, and snippets.

@euske
Last active September 13, 2021 04:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save euske/7a6724afef8d92b67eab96ce95f3d998 to your computer and use it in GitHub Desktop.
Save euske/7a6724afef8d92b67eab96ce95f3d998 to your computer and use it in GitHub Desktop.
sshon: add a specified ssh key only when necessary.
#!/bin/sh
SSH_HOME=$HOME/.ssh
SSH_LOG=${SSH_AGENT_LOG:-$SSH_HOME/ssh-agent.log}
SSH_KEY=${1:-$SSH_HOME/id_ed25519}
KEY_DURATION=1d
IFS=$'\n'
sig=$(ssh-keygen -l -f "$SSH_KEY")
for i in $(ssh-add -l); do
if [ "$i" = "$sig" ]; then
echo "$sig"
exit
fi
done
ssh-add -t "$KEY_DURATION" "$SSH_KEY" &&
echo "$(date) added: $SSH_KEY" >>$SSH_LOG;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment