Skip to content

Instantly share code, notes, and snippets.

@mather
Created November 26, 2013 02:20
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 mather/7652534 to your computer and use it in GitHub Desktop.
Save mather/7652534 to your computer and use it in GitHub Desktop.
fabricで鍵認証をセットする ref: http://qiita.com/mather314/items/d8db37ab6aaaaccdabba
from fabric.api import *
@task
def copy_id(file="~/.ssh/id_rsa.pub"):
"""Identityをauthorized_keysに追加する"""
put(file, "/tmp/id.pub")
try:
run("if [ ! -d ~/.ssh ]; then mkdir -p ~/.ssh; fi")
run("if [ ! -f ~/.ssh/authorized_keys ]; then cp /tmp/id.pub ~/.ssh/authorized_keys && chmod 0600 ~/.ssh/authorized_keys; fi")
run("cat ~/.ssh/authorized_keys /tmp/id.pub | sort -u > /tmp/uniq.authorized_keys")
run("if [ `cat ~/.ssh/authorized_keys | wc -l` -lt `cat /tmp/uniq.authorized_keys | wc -l` ]; then cat /tmp/id.pub >> ~/.ssh/authorized_keys; fi")
finally:
run("rm -f /tmp/id.pub /tmp/uniq.authorized_keys")
fab -H target copy_id:file=~/.ssh/custom_id.pub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment