My expect script

#!/usr/bin/expect -f

#I tried replacing sh - with bash -s still no positive results
spawn ssh xxxx@yyyy "sh -" < test.sh
expect "password: "
send "zzzzz\r"
expect "$ "

This command works well if executed in the terminal

ssh xxxx@yyyy "sh -" < test.sh

But if I execute it via expect script; it fails.

This is the output if I execute it via the expect script. May I know where I am going wrong

bash: test.sh: No such file or directory

P.S : Yes, the file exists and the credentials are right.

closed as off-topic by Jeff Schaller, don_crissti, slm Jun 30 '16 at 15:13

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question has been posted on multiple sites. Cross-posting is strongly discouraged; see the help center and community FAQ for more information." – Jeff Schaller, don_crissti, slm


The problem is that the redirection with < is being done locally when you run ssh on its own (by the shell you are typing into), but being done on the remote machine when you run from expect, because spawn simply copies the arguments you provide, so the < is passed to the remote as part of the ssh command. It is as if you typed 'ssh' 'xxxx@yyyy' "sh -" '<' 'test.sh' to the shell, which similarly will not work.

You need to add an intermediate shell command to get the redirection parsed before the ssh. Use for example,

spawn sh -c "ssh xxxx@yyyy sh - < test.sh"

Not the answer you're looking for? Browse other questions tagged or ask your own question.