65.9K
CodeProject 正在变化。 阅读更多。
Home

将所有 GitHub 密钥添加为授权用户

2015 年 10 月 27 日

MIT
viewsIcon

4414

将所有 GitHub 密钥添加为授权用户的一行命令,用于测试 Unix 服务器

引言

作为一名软件开发者,我经常使用大量的虚拟环境进行测试。我做的第一步 - 就是从密码 SSH 授权切换到公钥授权。让我与你分享一个方便的批处理命令,它可以将所有 GitHub 密钥添加为授权用户。

批处理

USERNAME=yourgithubusername

mkdir -p ~/.ssh

if ! [[ -f ~/.ssh/authorized_keys ]]; then
  echo "Creating new ~/.ssh/authorized_keys"
  touch ~/.ssh/authorized_keys
fi

keys=`curl https://api.github.com/users/$USERNAME/keys | grep -o -E "ssh-\w+\s+[^\"]+"`

for key in $keys; do
  echo $key
  grep -q "$key" ~/.ssh/authorized_keys || echo "$key" >> ~/.ssh/authorized_keys
done

用法

通常我这样运行它:

  curl -L http://bit.ly/easytoremembershortcut | bash -s

这种方法已经为我节省了大量时间。

© . All rights reserved.