0

I execute the following commands a few times a day:

ssh-keygen -t rsa -N "" -C "[email protected]" -f ~/.ssh/id_rsa_projectname
eval `ssh-agent`
ssh-add ~/.ssh/id_rsa_projectname
cat ~/.ssh/id_rsa_projectname.pub
ssh -T [email protected]

The only variable in this script is the projectname, I would like to make a keygen.sh script or something like that to automate this process and pass along the projectname. Is this possible?

Also where should I start looking and what not to forget, I'm a bit new to bash scripting and I know it can be quite dangerous in the wrong hands.

2
  • You generate deploy keys multiple times every day? Why? Commented Dec 2, 2015 at 20:26
  • @Chris two or three times a day when i clone a new repo from Github to a new staging or developer enviroment. Commented Dec 2, 2015 at 20:36

1 Answer 1

1

Would it not be easier to just maintain a single set of staging or development keys rather than generating them for everything? IMHO you're losing configurability and not gaining much in security.

That aside, you're on the right track but I would do things a bit different.

export PROJECT=foo;
ssh-keygen -t rsa -N "" -C "[email protected]" -f ~/.ssh/id_rsa_${PROJECT}

That will generate named keys id_rsa_foo and id_rsa_foo.pub Now you need to make your ssh config use it for github. ~/.ssh/config should have something like:

Host            remote github.com
IdentityFile    ~/.ssh/id_rsa_foo
User            git
StrictHostKeyChecking no

You'll need to upload the public key to github. You'll have to figure this out for yourself using their API.

If you do all this correctly you should be able to git clone automagically.

#!/bin/bash
[[ -z "${PROJECT}" ]] && echo "project must be set" && exit 1
ssh-keygen -t rsa -N "" -C "[email protected]" -f ~/.ssh/id_rsa_${PROJECT}
chmod 400 ~/.ssh/id_rsa_${PROJECT}
echo $'     Host            remote github.com\n    IdentityFile     ~/.ssh/id_rsa_'${PROJECT}'\n    User            git\n      StrictHostKeyChecking no' >> ~/.ssh/config
chmod 644 ~/.ssh/config
# do the github api stuff to add the pub key 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.