amazon-ssm-agent icon indicating copy to clipboard operation
amazon-ssm-agent copied to clipboard

Piping commands to start-session causes infinite loop of shell prompts.

Open phene opened this issue 7 years ago • 7 comments

I'm attempting to write a wrapper script to start an ssm session in a bash prompt using the following:

echo '/bin/bash' | aws ssm start-session --target $instance_id

When I do this, I get an infinite loop of bash prompts and must terminate the process externally. The session is also left open and must be terminated manually with aws ssm terminate-session Similarly, if I pipe something like echo hello, I see "hello" printed, but then get the infinite loop of shell prompts.

Only echo 'exit' | aws ssm start-session doesn't exhibit this behavior, instead exiting immediately.

phene avatar Jan 11 '19 19:01 phene

@phene The pipe is closed while ssm is not so that's causing problems. Luckily this works: https://stackoverflow.com/a/5852578/2115135

(echo '/bin/bash' && cat) | aws ssm start-session --target $instance_id

blckct avatar Jan 31 '19 15:01 blckct

FYI, that works great, just don't ever try to exit the shell with ctrl+d, otherwise you end up with the infinite loop of prompts that I described before.

Exiting the shell without the command piping using ctrl+d works as normal.

phene avatar Jan 31 '19 15:01 phene

@phene Yeah, that probably could be fixed by killing the aws cli on the client side after the cat command exits but I don't have a ready snippet for that one. Or you could try expect.

blckct avatar Jan 31 '19 17:01 blckct

This seems to work in the case of ctrl-d:

(echo '/bin/bash' && cat && echo 'exit' && echo 'exit') | aws ssm start-session --target $instance_id

Both exits are needed (1 for /bin/bash, the other for /bin/sh). The problem is other control commands are totally broken. If I run something like top after starting the session, exiting with ctrl-c gives me the infinite prompt issue again. My guess is that cat is receiving and processing the ctrl-c rather than top.

phene avatar Jan 31 '19 18:01 phene

Thank you for the feedback! We will look into this.

sahirangaraju avatar Apr 09 '19 22:04 sahirangaraju

I was able to get the ctrl-c stuff to work along with other items by adding stty raw -echo ; prior to the (...) and then after the aws ssm start-session... I add ; reset to reset my tty. The only issue I am really running into is the cat command hanging and having to hit a key.

stephenl03 avatar Jan 29 '20 18:01 stephenl03

You can now use Shell Profile to start in bash. https://aws.amazon.com/blogs/mt/bring-your-own-cli-session-manager-configurable-shell-profiles/ Closing.

ferkhat-aws avatar Feb 19 '21 20:02 ferkhat-aws