2019-06-08 17:10:08 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
class SSHKey(object):
|
|
|
|
def __init__(self, parser, parents):
|
|
|
|
self.parser = parser
|
|
|
|
|
|
|
|
self.parser['sshkey'] = self.parser['sub'].add_parser(
|
|
|
|
'sshkey',
|
|
|
|
help="Manage SSH keys",
|
|
|
|
parents=[parents])
|
|
|
|
|
|
|
|
self.parser['sshkey'].add_argument('--user',
|
|
|
|
help='Username on the host',
|
|
|
|
required=False,
|
|
|
|
default="root")
|
|
|
|
self.parser['sshkey'].add_argument('--key',
|
|
|
|
help='Name of the key',
|
|
|
|
required=True)
|
|
|
|
self.parser['sshkey'].add_argument('--key-dir',
|
|
|
|
help='Directory holding keys',
|
2019-06-08 17:18:37 +00:00
|
|
|
default=os.path.join(os.environ['HOME'], "vcs/ungleich-ssh-keys/current"),
|
|
|
|
required=False)
|
2019-06-08 17:10:08 +00:00
|
|
|
self.parser['sshkey'].add_argument('--host',
|
|
|
|
help='Host to use',
|
|
|
|
required=True)
|
|
|
|
self.parser['sshkey'].add_argument('--delete',
|
|
|
|
help='Delete key instead of adding',
|
|
|
|
action="store_true")
|
|
|
|
self.parser['sshkey'].set_defaults(func=self._manage_key)
|
|
|
|
|
|
|
|
|
|
|
|
def _manage_key(self, args):
|
|
|
|
if args.delete:
|
|
|
|
print("Not yet supported to delete, sorry :-)")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2019-06-08 17:18:37 +00:00
|
|
|
keyfile = os.path.join(args.key_dir, "{}.pub".format(args.key))
|
2019-06-08 17:10:08 +00:00
|
|
|
|
|
|
|
if not os.path.exists(keyfile):
|
2019-06-08 17:18:37 +00:00
|
|
|
print("Key for {} does not exist in {}. Aborting".format(args.key, args.key_dir))
|
2019-06-08 17:10:08 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
2019-06-08 17:18:37 +00:00
|
|
|
cmd = 'cat {} | ssh {}@{} "mkdir -p ~/.ssh; cat >> ~/.ssh/authorized_keys"'.format(keyfile,
|
|
|
|
args.user,
|
|
|
|
args.host)
|
2019-06-08 17:10:08 +00:00
|
|
|
|
|
|
|
subprocess.run(cmd, shell=True)
|