def add(name, connection_uri, id_file="", o=[], config=None): """ Adds a new entry to sshconfig. """ storm_ = get_storm_instance(config) try: # validate name if '@' in name: raise ValueError('invalid value: "@" cannot be used in name.') user, host, port = parse( connection_uri, user=get_default("user", storm_.defaults), port=get_default("port", storm_.defaults) ) storm_.add_entry(name, host, user, port, id_file, o) print(get_formatted_message('{0} added to your ssh config. you can connect it by typing "ssh {0}".'.format( name ), 'success')) except ValueError as error: print(get_formatted_message(error, 'error'), file=sys.stderr)
def add(name, connection_uri, id_file="", o=[], config=None): """ Adds a new entry to sshconfig. """ storm_ = get_storm_instance(config) try: # validate name if '@' in name: raise ValueError('invalid value: "@" cannot be used in name.') user, host, port = parse(connection_uri, user=get_default("user", storm_.defaults), port=get_default("port", storm_.defaults)) storm_.add_entry(name, host, user, port, id_file, o) print( get_formatted_message( '{0} added to your ssh config. you can connect ' 'it by typing "ssh {0}".'.format(name), 'success')) except ValueError as error: print(get_formatted_message(error, 'error'), file=sys.stderr)
def test_uri_parser(self): user = getpass.getuser() TEST_STRINGS = [('[email protected]:22', ('root', 'emreyilmaz.me', 22)), ('emreyilmaz.me', (user, 'emreyilmaz.me', 22)), ('emreyilmaz.me:22', (user, 'emreyilmaz.me', 22)), ('*****@*****.**', ('root', 'emreyilmaz.me', 22))] for uri in TEST_STRINGS: self.assertEqual(parse(uri[0]), uri[1]) # false strings self.assertRaises(ValueError, parse, '[email protected]:string-port')
def test_uri_parser(self): user = getpass.getuser() TEST_STRINGS = [ ('[email protected]:22', ('root', 'emreyilmaz.me', 22)), ('emreyilmaz.me', (user, 'emreyilmaz.me', 22)), ('emreyilmaz.me:22', (user, 'emreyilmaz.me', 22)), ('*****@*****.**', ('root', 'emreyilmaz.me', 22)) ] for uri in TEST_STRINGS: self.assertEqual(parse(uri[0]), uri[1]) # false strings self.assertRaises(ValueError, parse, '[email protected]:string-port')
def edit(): storm_ = app.get_storm() try: name = request.json["name"] connection_uri = request.json["connection_uri"] if "id_file" in request.json: id_file = request.json["id_file"] else: id_file = "" user, host, port = parse(connection_uri) storm_.edit_entry(name, host, user, port, id_file) return response() except ValueError as exc: return jsonify(message=exc.message), 404 except (KeyError, TypeError): return response(status=400)
def edit(): storm_ = app.get_storm() try: name = request.json['name'] connection_uri = request.json['connection_uri'] if 'id_file' in request.json: id_file = request.json['id_file'] else: id_file = '' user, host, port = parse(connection_uri) storm_.edit_entry(name, host, user, port, id_file) return response() except ValueError as exc: return jsonify(message=exc.message), 404 except (KeyError, TypeError): return response(status=400)
def edit(): storm_ = app.get_storm() try: name = request.json['name'] connection_uri = request.json['connection_uri'] id_file = None if 'id_file' in request.json: id_file = request.json['id_file'] if id_file == '': id_file = DELETED_SIGN user, host, port = parse(connection_uri) storm_.edit_entry(name, host, user, port, id_file) return response() except ValueError as exc: return jsonify(message=exc.message), 404 except (KeyError, TypeError): return response(status=400)
def edit_(name, connection_uri, id_file, options, config): options = [i for i in options] storm_ = get_storm_instance(config) try: if "," in name: name = " ".join(name.split(",")) user, host, port = parse( connection_uri, user=get_default("user", storm_.defaults), port=get_default("port", storm_.defaults), ) storm_.edit_entry(name, host, user, port, id_file, options) cprint(f"'{name}' updated successfully.", "success") except ValueError as error: cprint(error, "error") sys.exit(1)
def edit(name, connection_uri, id_file="", o=[], config=None): """ Edits the related entry in ssh config. """ storm_ = get_storm_instance(config) try: if ',' in name: name = " ".join(name.split(",")) user, host, port = parse(connection_uri, user=get_default("user", storm_.defaults), port=get_default("port", storm_.defaults)) storm_.edit_entry(name, host, user, port, id_file, o) print( get_formatted_message('"{0}" updated successfully.'.format(name), 'success')) except ValueError as error: print(get_formatted_message(error, 'error'), file=sys.stderr)
def add_(name, connection_uri, copy_id, id_file, options, config): options = [i for i in options] storm_ = get_storm_instance(config) try: if "@" in name: raise InvalidValueError user, host, port = parse( connection_uri, user=get_default("user", storm_.defaults), port=get_default("port", storm_.defaults), ) storm_.add_entry(name, host, user, port, id_file, options) try: if copy_id: if ping(host): ret = ssh_copy_id(name) if ret: cprint(f"ssh key added to host '{name}'", "success") else: cprint( f"Could not add ssh key to host '{name}', The host is not reachable", "error", ) except Exception as error: cprint(str(error), "error") sys.exit(1) cprint( f"'{name}' added to your ssh config. " f"you can connect to it by typing \nssh {name}", "success", ) except ValueError as error: cprint(str(error), "error") sys.exit(1)
def add(): storm_ = app.get_storm() try: name = request.json['name'] connection_uri = request.json['connection_uri'] id_file = None if 'id_file' in request.json: id_file = request.json['id_file'] if '@' in name: msg = 'invalid value: "@" cannot be used in name.' return jsonify(message=msg), 400 user, host, port = parse(connection_uri) storm_.add_entry(name, host, user, port, id_file) return response(status=201) except ValueError as exc: return jsonify(message=exc.message) except (KeyError, TypeError): return response(status=400)
def execute_host(args): if not args.subcommand == 'list' and not args.host: puts(red('--host is required')) return ssh_config = Storm() if args.subcommand == 'list': for entry in ssh_config.list_entries(): puts(green(entry['host'])) elif args.subcommand == 'add': result = parse(args.connection_url) puts(result) ssh_config.add_entry(args.host, host=result[1], user=result[0], port=result[2], id_file=args.id_file) for entry in ssh_config.list_entries(): puts(green(entry['host'])) elif args.subcommand == 'delete': ssh_config.delete_entry(args.host) for entry in ssh_config.list_entries(): puts(green(entry['host']))
def edit(name, connection_uri, id_file="", o=[], config=None): """ Edits the related entry in ssh config. """ storm_ = get_storm_instance(config) try: if ',' in name: name = " ".join(name.split(",")) user, host, port = parse( connection_uri, user=get_default("user", storm_.defaults), port=get_default("port", storm_.defaults) ) storm_.edit_entry(name, host, user, port, id_file, o) print(get_formatted_message( '"{0}" updated successfully.'.format( name ), 'success')) except ValueError as error: print(get_formatted_message(error, 'error'), file=sys.stderr)
def parse_connection_uri(self, connection_uri): options = dict() options["user"], options["hostname"], options["port"] = parse( connection_uri) return options
def parse_connection_uri(self, connection_uri): options = dict() options["user"], options["hostname"], options["port"] = parse(connection_uri) return options