def test_basic_login(self): async def run_client(): async with asyncssh.connect('localhost', port=8888, username='******', password='******', known_hosts=None, loop=self.loop) as _: pass ssh_key_file = 'ssh.key' SSH.generate_ssh_key(ssh_key_file) options = {'enabled': 'True', 'port': 8888} server_coro = asyncssh.create_server(lambda: SSH(options, self.loop), '0.0.0.0', 8888, server_host_keys=['ssh.key'], loop=self.loop) self.server = self.loop.run_until_complete(server_coro) try: self.loop.run_until_complete(run_client()) except asyncssh.Error: pass
def sftp_server(loop, host_key): server = asyncssh.create_server(SSHServer, host='', port=8022, loop=loop, sftp_factory=SFTPServer, server_host_keys=[host_key]) loop.run_until_complete(server)
def create_server(cls, server_factory=(), server_host_keys=(), **kwargs): """Create an SSH server for the tests to use""" if server_factory is (): server_factory = Server if server_host_keys is (): server_host_keys = ['skey'] return (yield from asyncssh.create_server( server_factory, port=0, server_host_keys=server_host_keys, **kwargs))
def main(port=8222): # Set up logging. logging.basicConfig() logging.getLogger().setLevel(logging.DEBUG) loop = asyncio.get_event_loop() loop.run_until_complete( asyncssh.create_server( lambda: PromptToolkitSSHServer(interact), "", port, server_host_keys=["/etc/ssh/ssh_host_ecdsa_key"], )) loop.run_forever()
def main(port=8222): # Set up logging. logging.basicConfig() logging.getLogger().setLevel(logging.DEBUG) loop = asyncio.get_event_loop() loop.run_until_complete( asyncssh.create_server( lambda: PromptToolkitSSHServer(interact), "", port, server_host_keys=["/etc/ssh/ssh_host_ecdsa_key"], ) ) loop.run_forever()
def create_server(cls, server_factory=(), *, loop=(), server_host_keys=(), **kwargs): """Create an SSH server for the tests to use""" if loop is (): loop = cls.loop if server_factory is (): server_factory = Server if server_host_keys is (): server_host_keys = ['skey'] return (yield from asyncssh.create_server( server_factory, port=0, family=socket.AF_INET, loop=loop, server_host_keys=server_host_keys, **kwargs))
def __call__(self): database = Service.resolve("moxie.cores.database.DatabaseService") # self.alert = CronService.resolve("moxie.cores.alert.AlertService") # register an ssh callback for each thinger ssh_host_keys = asyncssh.read_private_key_list('ssh_host_keys') if MoxieSSHServer._keys is None: authorized_keys = {} for key in asyncssh.read_public_key_list('authorized_keys'): authorized_keys[key] = (yield from database.user.get_by_fingerprint( fingerprint(key))) MoxieSSHServer._keys = authorized_keys obj = yield from asyncssh.create_server( MoxieSSHServer, '0.0.0.0', 2222, server_host_keys=ssh_host_keys ) return obj
def main(port=8222): """ Example that starts the REPL through an SSH server. """ loop = asyncio.get_event_loop() # Namespace exposed in the REPL. environ = {'hello': 'world'} # Start SSH server. def create_server(): return MySSHServer(lambda: environ) print('Listening on :%i' % port) print('To connect, do "ssh localhost -p %i"' % port) loop.run_until_complete( asyncssh.create_server(create_server, '', port, server_host_keys=['/etc/ssh/ssh_host_dsa_key'])) # Run eventloop. loop.run_forever()
def start_server(): yield from asyncssh.create_server(MySSHServer, '', 8022, server_host_keys=host_keys)
def start(self): """ Starts services. """ if 'public_ip_as_destination_ip' in self.config and self.config['public_ip_as_destination_ip'] is True: asyncio.ensure_future(self._record_and_lookup_public_ip(), loop=self.loop) # start activity logging if 'activity_logging' in self.config: if 'file' in self.config['activity_logging'] and self.config['activity_logging']['file']['enabled']: auth_log = self.config['activity_logging']['file']['authentication_log_file'] session_csv_log = self.config['activity_logging']['file']['session_csv_log_file'] session_json_log = self.config['activity_logging']['file']['session_json_log_file'] file_logger = FileLogger(session_csv_log, session_json_log, auth_log) self.file_logger_task = self.loop.run_in_executor(None, file_logger.start) self.file_logger_task.add_done_callback(common.on_unhandled_task_exception) self._loggers.append(file_logger) if 'syslog' in self.config['activity_logging'] and self.config['activity_logging']['syslog']['enabled']: sys_logger = SyslogLogger() self.sys_logger_task = self.loop.run_in_executor(None, sys_logger.start) self.sys_logger_task.add_done_callback(common.on_unhandled_task_exception) self._loggers.append(sys_logger) if 'hpfeeds' in self.config['activity_logging'] and self.config['activity_logging']['hpfeeds']['enabled']: session_channel = self.config['activity_logging']['hpfeeds']['session_channel'] auth_channel = self.config['activity_logging']['hpfeeds']['auth_channel'] host = self.config['activity_logging']['hpfeeds']['host'] port = self.config['activity_logging']['hpfeeds']['port'] ident = self.config['activity_logging']['hpfeeds']['ident'] secret = self.config['activity_logging']['hpfeeds']['secret'] hpfeeds_logger = HpFeedsLogger(session_channel, auth_channel, host, port, ident, secret) self.hpfeeds_logger_task = self.loop.run_in_executor(None, hpfeeds_logger.start) self.hpfeeds_logger_task.add_done_callback(common.on_unhandled_task_exception) if 'curiosum' in self.config['activity_logging'] and self.config['activity_logging']['curiosum']['enabled']: port = self.config['activity_logging']['curiosum']['port'] curiosum_integration = CuriosumIntegration(port) self.hpfeeds_logger_task = self.loop.run_in_executor(None, curiosum_integration.start) self.hpfeeds_logger_task.add_done_callback(common.on_unhandled_task_exception) bind_host = self.config['bind_host'] listen_ports = [] for c in heralding.capabilities.handlerbase.HandlerBase.__subclasses__(): cap_name = c.__name__.lower() if cap_name in self.config['capabilities']: if not self.config['capabilities'][cap_name]['enabled']: continue port = self.config['capabilities'][cap_name]['port'] listen_ports.append(port) # carve out the options for this specific service options = self.config['capabilities'][cap_name] # capabilities are only allowed to append to the session list cap = c(options, self.loop) try: # # Convention: All capability names which end in 's' will be wrapped in ssl. if cap_name.endswith('s'): pem_file = '{0}.pem'.format(cap_name) self.create_cert_if_not_exists(cap_name, pem_file) ssl_context = self.create_ssl_context(pem_file) server_coro = asyncio.start_server(cap.handle_session, bind_host, port, loop=self.loop, ssl=ssl_context) elif cap_name == 'ssh': # Since dicts and user-defined classes are mutable, we have # to save ssh class and ssh options somewhere. ssh_options = options SshClass = c self.SshClass = SshClass ssh_key_file = 'ssh.key' SshClass.generate_ssh_key(ssh_key_file) banner = ssh_options['protocol_specific_data']['banner'] SshClass.change_server_banner(banner) server_coro = asyncssh.create_server(lambda: SshClass(ssh_options, self.loop), bind_host, port, server_host_keys=[ssh_key_file], login_timeout=cap.timeout, loop=self.loop) else: server_coro = asyncio.start_server(cap.handle_session, bind_host, port, loop=self.loop) server = self.loop.run_until_complete(server_coro) logger.debug('Adding %s capability with options: %s', cap_name, options) self._servers.append(server) except Exception as ex: error_message = "Could not start {0} server on port {1}. Error: {2}".format(c.__name__, port, ex) logger.error(error_message) self.loop.run_until_complete(common.cancel_all_pending_tasks(self.loop)) sys.exit(error_message) else: logger.info('Started %s capability listening on port %s', c.__name__, port) ReportingRelay.logListenPorts(listen_ports)
def start_server(): yield from asyncssh.create_server(MySSHServer, '', 8022, server_host_keys=['ssh_host_key'], authorized_client_keys='ssh_user_ca')
def start_server(): yield from asyncssh.create_server(MySSHServer, "", 8022, server_host_keys=["ssh_host_key"])
def start_server(): yield from asyncssh.create_server( MySSHServer, "", 8022, server_host_keys=["ssh_host_key"], authorized_client_keys="ssh_user_ca" )
def start(self): """ Starts services. """ if 'public_ip_as_destination_ip' in self.config and self.config[ 'public_ip_as_destination_ip'] is True: asyncio.ensure_future(self._record_and_lookup_public_ip(), loop=self.loop) # start activity logging if 'activity_logging' in self.config: if 'file' in self.config['activity_logging'] and self.config[ 'activity_logging']['file']['enabled']: log_file = self.config['activity_logging']['file']['filename'] file_logger = FileLogger(log_file) self.file_logger_task = self.loop.run_in_executor( None, file_logger.start) self.file_logger_task.add_done_callback( common.on_unhandled_task_exception) self._loggers.append(file_logger) if 'syslog' in self.config['activity_logging'] and self.config[ 'activity_logging']['syslog']['enabled']: sys_logger = SyslogLogger() self.sys_logger_task = self.loop.run_in_executor( None, sys_logger.start) self.sys_logger_task.add_done_callback( common.on_unhandled_task_exception) self._loggers.append(sys_logger) for c in heralding.capabilities.handlerbase.HandlerBase.__subclasses__( ): cap_name = c.__name__.lower() if cap_name in self.config['capabilities']: if not self.config['capabilities'][cap_name]['enabled']: continue port = self.config['capabilities'][cap_name]['port'] # carve out the options for this specific service options = self.config['capabilities'][cap_name] # capabilities are only allowed to append to the session list cap = c(options, self.loop) try: # # Convention: All capability names which end in 's' will be wrapped in ssl. if cap_name.endswith('s'): pem_file = '{0}.pem'.format(cap_name) self.create_cert_if_not_exists(cap_name, pem_file) ssl_context = self.create_ssl_context(pem_file) server_coro = asyncio.start_server(cap.handle_session, '0.0.0.0', port, loop=self.loop, ssl=ssl_context) elif cap_name == 'ssh': # Since dicts and user-defined classes are mutable, we have # to save ssh class and ssh options somewhere. ssh_options = options SshClass = c self.SshClass = SshClass ssh_key_file = 'ssh.key' SshClass.generate_ssh_key(ssh_key_file) banner = ssh_options['protocol_specific_data'][ 'banner'] SshClass.change_server_banner(banner) server_coro = asyncssh.create_server( lambda: SshClass(ssh_options, self.loop), '0.0.0.0', port, server_host_keys=[ssh_key_file], login_timeout=cap.timeout, loop=self.loop) else: server_coro = asyncio.start_server(cap.handle_session, '0.0.0.0', port, loop=self.loop) server = self.loop.run_until_complete(server_coro) logger.debug( 'Adding {0} capability with options: {1}'.format( cap_name, options)) self._servers.append(server) except Exception as ex: error_message = "Could not start {0} server on port {1}. Error: {2}".format( c.__name__, port, ex) logger.error(error_message) task_killer = common.cancel_all_pending_tasks(self.loop) self.loop.run_until_complete(task_killer) sys.exit(error_message) else: logger.info( 'Started {0} capability listening on port {1}'.format( c.__name__, port))