fabric 是部署好帮手 下面的代码实现 ssh-copy-id 的功能 批量上传你的 id_rsa.pub 到一组服务器
@task def copy_id(file="~/.ssh/id_rsa.pub"): """fab push 公钥 ssh-copy-id""" 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")