Ожидание Python: команда выполняется только после отправки последующих командPython

Программы на Python
Ответить
Anonymous
 Ожидание Python: команда выполняется только после отправки последующих команд

Сообщение Anonymous »

Цель*
Я пытаюсь использовать команду aws ecsexecute в сочетании с pexpect.spawn(), чтобы запустить интерактивный сеанс bash в контейнер, работающий на экземпляре EC2.
Пример кода

Код: Выделить всё

import sys
import time

import boto3

import pexpect
import pexpect.expect
import pexpect.replwrap

def spawn_session(container, family, cluster, ecs_client):
print(f'Spawning Session: {container}, {family}, {cluster}')
rsp = ecs_client.list_tasks(cluster=cluster, family=family)
print(rsp)
task_arn = rsp.get('taskArns')[0]
local_argv = [
'aws', 'ecs', 'execute-command',
'--command', '/bin/bash',
'--interactive', '--task', task_arn, '--container', container,
'--cluster', cluster
]
ps = pexpect.spawn(' '.join(local_argv), timeout=900, logfile=sys.stdout.buffer)
ps.expect('ecs-execute-command.*# ', timeout=15)

return ps

def repl_session_example(container, family, cluster, ecs_client):
rsp = ecs_client.list_tasks(cluster=cluster, family=family)
print(rsp)
task_arn = rsp.get('taskArns')[0]
# bash = pexpect.replwrap.bash()
local_argv = [
'aws', 'ecs', 'execute-command',
'--command', '/bin/bash',
'--interactive', '--task', task_arn, '--container', container,
'--cluster', cluster
]
bash = pexpect.replwrap.REPLWrapper(' '.join(local_argv), '#', None)

return bash

def bash_session_example(container, family, cluster, ecs_client):
rsp = ecs_client.list_tasks(cluster=cluster, family=family)
print(rsp)
task_arn = rsp.get('taskArns')[0]
local_argv = [
'aws', 'ecs', 'execute-command',
'--command', '/bin/bash',
'--interactive', '--task', task_arn, '--container', container,
'--cluster', cluster
]
bash = pexpect.replwrap.bash()
bash.run_command(' '.join(local_argv))

return bash

if __name__ == '__main__':
ecs = boto3.client('ecs')
container = 'CONTAINER'
family = 'TASK_FAMILY'
cluster = 'CLUSTER_ARN'
# bash = repl_session_example(container, family, cluster, ecs)
# bash = bash_session_example(container, family, cluster, ecs)
bash = spawn_session(container, family, cluster, ecs)
for cmd in ['pwd', 'python --version', 'sleep 5', 'echo 2']:
# print(f'Sending: {cmd}')
bash.sendline(cmd)
bash.expect('# ')
# print(f'before: {bash.before}')
# print(f'after: {bash.after}')
# out = bash.run_command(cmd)
# print(out)
pass
Результаты
Вывод кажется неправильным, и мне приходится отправлять дополнительные входные данные, чтобы фактически получить выходные данные предыдущей команды.

Код: Выделить всё

# python temp.py
Spawning Session: CONTAINER, FAMILY, CLUSTER

...

The Session Manager plugin was installed successfully. Use the AWS CLI to start a session.

Starting session with SessionId: ecs-execute-command-
bash-4.2# pwd     

Подробнее здесь: [url]https://stackoverflow.com/questions/79148882/python-pexpect-command-only-happens-after-subsequent-commands-are-sent[/url]
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»