Snippets

Unit 193 ssh-import-id

Created by Unit 193
#!/usr/bin/python3
#
# ssh-import-id - Authorize SSH public keys from trusted online identities.
#
# Copyright (c) 2013 Casey Marshall <casey.marshall@gmail.com>
# Copyright (c) 2013 Dustin Kirkland <dustin.kirkland@gmail.com>
#
# ssh-import-id is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
#
# ssh-import-id is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ssh-import-id.  If not, see <http://www.gnu.org/licenses/>.

import os
import json
import subprocess
import sys
import requests
import getpass
from urllib.parse import quote_plus


if __name__ == '__main__':
	if not sys.argv[:1]:
		sys.stderr.write("Usage: %s <Bitbucket-id>\n" % (sys.argv[0]))
		os._exit(1)
	for bbid in sys.argv[1:]:
		try:
			password = getpass.getpass('Bitbucket password for %s: ' % bbid)
			url = "https://bitbucket.org/api/1.0/users/%s/ssh-keys" % (quote_plus(bbid))
			text = requests.get(url, auth=(bbid, password),verify=True).text
			data = json.loads(text)
			for keyobj in data:
				sys.stdout.write("%s %s@bitbucket/%s\n" % (keyobj['key'], bbid, keyobj['label']))
			sys.stdout.flush()
		except (Exception,):
			e = sys.exc_info()[1]
			sys.stderr.write("ERROR: %s\n" % (str(e)))
			os._exit(1)
		sys.stdout.write("\n")
	os._exit(0)

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.