Skip to content

Instantly share code, notes, and snippets.

@wolfgang-noichl
Last active March 5, 2017 10:54
Show Gist options
  • Save wolfgang-noichl/7c77684b85b88031d6ef033561204da7 to your computer and use it in GitHub Desktop.
Save wolfgang-noichl/7c77684b85b88031d6ef033561204da7 to your computer and use it in GitHub Desktop.
ssh-copy-id - alike public SSH key upload for gitlab
#!/usr/bin/env python
"""
ssh-copy-id - alike public SSH key upload for gitlab
Needs python-gitlab, click.
"""
import click, gitlab, subprocess, re, sys, getpass, socket
from os.path import expanduser
@click.command()
@click.argument('[user@]gitlab.url')
def gitlabSshCopyId(**kwargs):
"""ssh-copy-id - alike public SSH key upload for gitlab"""
_ = kwargs['[user@]gitlab.url'].split('@')
host = _[-1]
u = _[0] if len(_)==2 else getpass.getuser()
gl = gitlab.Gitlab('https://{}/'.format(host),
email=u,
password=getpass.getpass('Enter Password for {}: '.format(u))
)
gl.auth()
#TODO maybe add some case for id_rsa not there...
gl.user.keys.create({'title': '{}@{}'.format(u, socket.gethostname()),
'key': open(expanduser('~')+'/.ssh/id_rsa.pub').read()})
if __name__ == '__main__':
gitlabSshCopyId()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment