def _telnet_runner(self, connection, task, killers, command, streams): if streams and streams['stdin']: stdin, stdout, stderr, conn = streams['stdin'], streams['stdout'], streams['stderr'], streams['connection'] log.debug('Reuse telnet connection') else: try: ip, port = parse_host(connection['ip'], 23) process = yield from asyncio.create_subprocess_exec('telnet', '-E', ip, str(port), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdin, stdout, stderr = process.stdin, process.stdout, process.stderr except Exception as ex: log.error("Cannot open telnet session", exc_info=True) self.db_log.error("Не удалось открыть telnet соединение", str(ex), 'connection', connection['_id']) return -999, "", {} streams = dict(stdin=stdin, stdout=stdout, stderr=stderr, connection=process) log.debug('Open new telnet connection host={}'.format(connection.get('ip'))) killers.append(process.terminate) try: stdin.write("{}\r".format(command).encode('utf-8')) results = yield from self._read_stream_with_ttl(stdout) except: log.error("Cannot communicate with TELNET-session", exc_info=True) return -999, "", {} log.debug('Run telnet command "{}", output:\n--\n{}--'.format(command, results)) return 0, results, streams
def _ssh_runner(self, connection, task, killers, command, streams): if streams and streams['stdin'].channel._session: stdin, stdout, stderr, conn = streams['stdin'], streams[ 'stdout'], streams['stderr'], streams['connection'] log.debug('Reuse ssh connection') else: try: ip, port = parse_host(connection['ip'], 22) conn, client = yield from asyncssh.create_connection( None, host=ip, port=port, username=connection.get('login'), password=connection.get('password'), server_host_keys=None) stdin, stdout, stderr = yield from conn.open_session( term_type='xterm-color', term_size=(80, 24)) except asyncio.CancelledError: log.error("Cannot open SSH-session", exc_info=False) self.db_log.error("Не удалось открыть ssh соединение", str(ex), 'connection', connection['_id']) return -999, "", {} except Exception as ex: log.error("Cannot open SSH-session", exc_info=True) self.db_log.error("Не удалось открыть ssh соединение", str(ex), 'connection', connection['_id']) return -999, "", {} streams = dict(stdin=stdin, stdout=stdout, stderr=stderr, connection=conn) log.debug('Open new ssh connectionm host={}'.format( connection.get('ip'))) killers.append(conn.close) try: stdin.write("{}\r".format(command)) results = yield from self._read_ssh_with_ttl(stdout) except: log.error("Cannot communicate with SSH-session", exc_info=True) return -999, "", {} log.debug('Run ssh command "{}", output:\n--\n{}--'.format( command, results)) return 0, results, streams
def _telnet_runner(self, connection, task, killers, command, streams): if streams and streams['stdin']: stdin, stdout, stderr, conn = streams['stdin'], streams[ 'stdout'], streams['stderr'], streams['connection'] log.debug('Reuse telnet connection') else: try: ip, port = parse_host(connection['ip'], 23) process = yield from asyncio.create_subprocess_exec( 'telnet', '-E', ip, str(port), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdin, stdout, stderr = process.stdin, process.stdout, process.stderr except Exception as ex: log.error("Cannot open telnet session", exc_info=True) self.db_log.error("Не удалось открыть telnet соединение", str(ex), 'connection', connection['_id']) return -999, "", {} streams = dict(stdin=stdin, stdout=stdout, stderr=stderr, connection=process) log.debug('Open new telnet connection host={}'.format( connection.get('ip'))) killers.append(process.terminate) try: stdin.write("{}\r".format(command).encode('utf-8')) results = yield from self._read_stream_with_ttl(stdout) except: log.error("Cannot communicate with TELNET-session", exc_info=True) return -999, "", {} log.debug('Run telnet command "{}", output:\n--\n{}--'.format( command, results)) return 0, results, streams
def _ssh_runner(self, connection, task, killers, command, streams): if streams and streams['stdin'].channel._session: stdin, stdout, stderr, conn = streams['stdin'], streams['stdout'], streams['stderr'], streams['connection'] log.debug('Reuse ssh connection') else: try: ip, port = parse_host(connection['ip'], 22) conn, client = yield from asyncssh.create_connection(None, host=ip, port=port, username=connection.get('login'), password=connection.get('password'), server_host_keys=None) stdin, stdout, stderr = yield from conn.open_session(term_type='xterm-color', term_size=(80, 24)) except asyncio.CancelledError: log.error("Cannot open SSH-session", exc_info=False) self.db_log.error("Не удалось открыть ssh соединение", str(ex), 'connection', connection['_id']) return -999, "", {} except Exception as ex: log.error("Cannot open SSH-session", exc_info=True) self.db_log.error("Не удалось открыть ssh соединение", str(ex), 'connection', connection['_id']) return -999, "", {} streams = dict(stdin=stdin, stdout=stdout, stderr=stderr, connection=conn) log.debug('Open new ssh connectionm host={}'.format(connection.get('ip'))) killers.append(conn.close) try: stdin.write("{}\r".format(command)) results = yield from self._read_ssh_with_ttl(stdout) except: log.error("Cannot communicate with SSH-session", exc_info=True) return -999, "", {} log.debug('Run ssh command "{}", output:\n--\n{}--'.format(command, results)) return 0, results, streams