Search code examples
pythonbashshellposixzsh

interact or cancel a command that is waiting for input in bash and zsh on macos


I need a way to cancel a command that is waiting on input or send input to it so it moves past the waiting for input stage here's the scenario

I have a script and in it I am running a python script called aws-login that will go away and try and log into aws with existing credentials, if the credentials are valid it moves on fine if however the credentials are no longer valid it prompts for a password and i want to either kill the process or send in a new line so it will fail and move onto the next part of the script

I know I got it working the way i wanted a couple of years ago before i had an incident with my laptop and some coffee before i was able to backup the script

at the moment the approach i have is this

    function kill_proc() {
      local line=''

      while IFS= read -r line; do
        #echo ' \n' > /proc/${line}/fd/0
        kill -9 "$line"
      done < /dev/fd/0
    }

    function aws_process_login() {
      local line=''

      while IFS= read -r line; do
        if echo "$line" | grep -Fq "ERROR: Cannot extract saml assertion"; then
          ps aux | grep -i aws-adfs | grep -Fv 'grep -i' | tr '\t' ' ' | tr -s ' ' | cut -d' ' -f2 | kill_proc
          echo "${line}" >> /tmp/_$$_aws_error.txt
          echo -ne '\r\033[0KPassword test for User: '
          break
        else
          echo "${line}" >> /tmp/_$$_aws_login.txt
          echo "test2"
        fi
        sleep 0.050
      done < '/dev/stdin'
    }


PYTHONUNBUFFERED="TRUE" PATH=$_PATH aws-adfs login --adfs-host=$ADFS_DOMAIN --adfs-ca-bundle=$ROOT_CA --profile="$1" --session-duration 3600 --s3-signature-version s3v4 2>/dev/fd/1 | aws_process_login

the main issue is when aws_process_login ends it causes the program to become suspended

zsh: suspended (tty input) PYTHONUNBUFFERED="TRUE" PATH=$_PATH aws-adfs login --adfs-ca-bundle=$ROOT_CA

and so the script just gets stuck there anyone have any ideas? i need it to work in both zsh and bash as some users of the script use bash but most users are on zsh


Solution

  • I found a solution, unfortunately it only works for this very specific scenario and so wont work with other programs.

    echo "${username}\n${password}" | PYTHONUNBUFFERED="TRUE" PATH=$_PATH aws-adfs login --adfs-host=$ADFS_DOMAIN --adfs-ca-bundle=$ROOT_CA --profile="$1" --session-duration 3600 --s3-signature-version s3v4 --stdin >/tmp/_$$_aws_login.txt 2>/tmp/_$$_aws_error.txt
    

    Apparently if you provide a blank password to aws-adfs with the --stdin option it will skip the password prompt (I was sure i had already tested this but apparently i hadn't)

    This works with that I want, I want a custom password prompt so on the first pass it skips asking for a password and tries to login and if it succeeds my script ends, if it fails then my script loops back around and asks for a password the subsequent times and passes it in via the password variable.