1

I have a program that runs from the command line. As soon as it runs, it asks for a text value and expects the return key to be pressed after that.

Is that possible to create a bash script that runs that program, wait a little bit for the prompt to appear (lets say 2 seconds) and then provide the text and the enter key?

EDIT:

I have created this script with expect but it is not working:

#!/usr/bin/expect -f

set timeout 15
set user "myusername"
set server "x.x.x.x"

spawn ssh -l $user -p AAAA $server
expect "myusername@x.x.x.x's password: "
send "the password\r"

where AAAA is the port and x.x.x.x the IP.

when I run this script it finishes almost immediately and nothing happens.

Duck
  • 4,674
  • 2
    Have you looked at expect? This is the sort of task it was made for – Eric Renouf Jun 11 '15 at 14:58
  • I have edited my question to show the script I have created using expect. First time I hear about expect. I was "expecting" the script to work, but it is not. – Duck Jun 11 '15 at 15:24
  • Try ending the send command with \r to "press enter" – Eric Renouf Jun 11 '15 at 15:27
  • sorry about that, I forgot to type \r on the question. The script has \r and is not working. – Duck Jun 11 '15 at 15:44
  • 1
    Is there any compelling reason why you can't use certificate-base authentication and dispense with the need for a password and its attendant complexity entirely? – Chris Davies Jun 11 '15 at 15:47
  • Are you sure your expect pattern matches exactly (including case) what you're getting back? – Eric Renouf Jun 11 '15 at 15:51
  • certificate-base authentication = no, because my boss give me that order and yes, the case matches completely. – Duck Jun 11 '15 at 16:13
  • I don't use expect much, but if my expect script wasn't working, I'd look at using the method here: http://www.cotse.com/dlf/man/expect/exp_internal.htm to find out what was going wrong. – Jeff Schaller Jun 11 '15 at 16:33
  • how do I use that inside the bash script? – Duck Jun 11 '15 at 16:46

1 Answers1

1

It is working now, but I changed the script to:

#!/usr/bin/expect -f

set timeout 15
set user "myusername"
set server "x.x.x.x"

spawn ssh -l $user -p AAAA $server
expect "myusername@x.x.x.x's password: " { send "the password\r" }
interact
Duck
  • 4,674