Skip to content

Instantly share code, notes, and snippets.

@mpdude
Last active June 26, 2022 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save mpdude/e56fcae5bc541b95187fa764aafb5e6d to your computer and use it in GitHub Desktop.
Save mpdude/e56fcae5bc541b95187fa764aafb5e6d to your computer and use it in GitHub Desktop.
Wrapper around `ssh` to pick the right one from several GitHub deploy keys
#!/bin/bash
# The last argument is the command to be executed on the remote end, which is something
# like "git-upload-pack 'webfactory/ssh-agent.git'". We need the repo path only, so we
# loop over this last argument to get the last part of if.
for last in ${!#}; do :; done
# Don't use "exec" to run "ssh" below; then the trap won't work.
key_file=$(mktemp -u)
trap "rm -f $key_file" EXIT
eval last=$last
# Try to pick the right key
ssh-add -L | grep --word-regexp --max-count=1 $last > $key_file
ssh -i $key_file "$@"
@mpdude
Copy link
Author

mpdude commented Sep 7, 2020

This wrapper script can be used when you need to use more than a single deploy key for GitHub.com. Keys can be kept in the ssh-agent, and the right key will identified through a key comment.

See https://www.webfactory.de/blog/using-multiple-ssh-deploy-keys-with-github.

@mpdude
Copy link
Author

mpdude commented Oct 8, 2020

Note to self: We could add this script somewhere to https://github.com/webfactory/ssh-agent.

Actions will be checked out in the worker, but we'd need to expand $HOME/work/_actions/webfactory/ssh-agent/*/....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment