2015-12-02 112 views
0

關鍵我執行以下命令的一天幾次:自動化生成部署github上

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] 

在該腳本的唯一的變量是projectname,我想作一個keygen.sh腳本或者類似的東西自動執行此過程並傳遞projectname。這可能嗎?

而且我應該在哪裏開始尋找,什麼不能忘了,我是一個有點新的bash腳本,我知道它可以在不法分子手中相當危險的。

+0

您每天都會多次生成部署密鑰?爲什麼? – Chris

+0

@Chris每天兩到三次,我從Github克隆一個新的回購到一個新的分期或開發環境。 – RTB

回答

1

難道不容易,只需維護一個集轉移或發展的關鍵,而不是生成它們的一切?恕我直言,你失去了可配置性,並沒有獲得太多的安全性。

一邊,你就是那個在正確的軌道上,但我會做的事情有點不同。

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

,將產生一個名爲鍵id_rsa_foo和id_rsa_foo.pub 現在你需要讓你的ssh配置使用它的github上。 ~/.ssh/config應該是這樣的:

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

您需要將公鑰上傳到github。你必須弄清楚自己使用他們的API。

如果你做這一切正常,你應該能夠,自動將克隆的git。

#!/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