paul paul - 4 months ago 16
Bash Question

Bash expect two commands in same process

I´m getting crazy trying to make this works.

I need to use expect to set a couple passwords. The problem that I have is that I have two run two commands in the same processor.

Here the code

yum install -y expect
secret="price_aws_git_secret.txt"
eval $(ssh-agent)
pass=$(cat $secret)
expect << EOF
spawn ssh-agent ssh-add price_aws_github
expect "Enter passphrase"
send "$pass\r"
spawn git clone git@github/repo.git
expect "Are you sure you want to continue connecting"
send "yes\r"
expect eof
EOF


The first command add into the ssh-agent the ssh key, and the second command the git clone need to be in the same process to get that agent.

Looking documentation and examples I´m not able to see how expect can works with two commands in the same process.

Any suggestion?

Thanks!

Answer

Suggestions about using ssh keys better aside, to answer your question. You don't need to interact with two processes at the same time. You need to handle the first one, then start the second:

expect << EOF
      spawn ssh-agent ssh-add price_aws_github
      expect "Enter passphrase"
      send "$pass\r"
      # assume this works. if not, there's more work to do
      expect eof
      close

      spawn git clone git@github/repo.git
      expect "Are you sure you want to continue connecting"
      send "yes\r"
      expect eof                 
EOF