Skip to content

Instantly share code, notes, and snippets.

@mahirchavda
Forked from eddieantonio/fabfile.py
Created March 28, 2020 11:29
Show Gist options
  • Save mahirchavda/e9461daddf5bcf67ea99b828da486a75 to your computer and use it in GitHub Desktop.
Save mahirchavda/e9461daddf5bcf67ea99b828da486a75 to your computer and use it in GitHub Desktop.
Fabric: task to authorize your SSH key on a remote host.
"""
Installs your SSH key on other hosts. A fabfile for lazy people.
"""
from fabric.api import task, run, put, env, cd
# Use sh instead of bash.
env.shell = '/bin/sh -l -c'
@task
def add_ssh_key(identity='~/.ssh/id_rsa.pub'):
# Copy the key over.
REMOTE_PATH = '~/id.pub'
put(identity, REMOTE_PATH)
with cd('~'):
# Make sure the SSH directory is created.
run('mkdir -p .ssh')
# And append to the authrized keys.
run('cat %(REMOTE_PATH)s >> ~/.ssh/authorized_keys' % locals())
# Be thourough and leave no trace of this interaction!
run('rm %(REMOTE_PATH)s' % locals())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment