0

The code below is adapted from a solution to "Use Expect in a Bash script to provide a password to an SSH command", so as to pass arguments to git push. I'm not getting any exceptions for passing the wrong uname+pwd, and conversely passing the correct ones does not actually push anything. How can this be corrected?

git_push.sh

if (( $# == 2 ))
then
    :
else
    echo "expecting 'username pass', got $@"
    exit 1
fi

user="$1"
pass="$2"
expect - <<EOF
 spawn git push
 expect 'User*'
 send "$user\r"
 expect 'Pass*'
 send "$pass\r"
EOF

Terminal:

$ [path]/git_push.sh
spawn git push
Username for 'https://github.com': foo
Password for 'https://[email protected]': 

Alternatively (no wildcards):

 spawn git push
 expect "Username for 'https://github.com': "
 send "$user\r"
 expect "Password for 'https://[email protected]': "
 send "$pass\r"
4
  • 2
    Why are you using expect rather than uploading a ssh key? If for some reason you can't, my next option would be sshpass, not expect.
    – frabjous
    Mar 10, 2022 at 3:59
  • @frabjous the ssh key is for authentication's sake. expect is for automating the execution of an interactive command. I don't think I'm getting mixed up.
    – Erwann
    Mar 10, 2022 at 6:02
  • 2
    I don't think I'm getting mixed up. You don't, but in your case, ssh-keys will replace yourexpect script as you do not need to provide username/PAT Mar 10, 2022 at 6:40
  • @NicolasFormichella OK, fair point: switch from 'https' to 'ssh' authentication (I did) so I don't have to rely on expect. But doesn't the question about the workings of expect retain some validity?
    – Erwann
    Mar 10, 2022 at 7:08

1 Answer 1

1

To address the questions:

expect - <<EOF
 spawn git push
 expect 'User*'
 send "$user\r"
 expect 'Pass*'
 send "$pass\r"
EOF
  1. single quotes have no special meaning in expect, so you are looking for literal single quotes in the User and Pass prompts. Those prompt will not contain single quotes, so the expect command hangs until the timeout (default 10 seconds) happens.

  2. after you send the password, you don't wait for the push to complete: the expect script runs out of commands to run and exits too early, killing the git process. After any send, you should expect something. In this case, you're expecting the spawned command to end which is denoted with expect eof

expect - <<_END_EXPECT
    spawn git push
    expect "User*"
    send "$user\r"
    expect "Pass*"
    send "$pass\r"
    set timeout -1  ; # no timeout
    expect eof
_END_EXPECT
3
  • Not quite for me: : wrong # args: should be "set varName ?newValue?" while executing "set timeout -1 # no timeout
    – Erwann
    Mar 10, 2022 at 20:10
  • Woops! There should be a semicolon before the hash. Updated. Mar 10, 2022 at 21:49
  • Tcl comments are weird: the comment character can only appear where a command can be valid. Mar 10, 2022 at 21:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .