0

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.

2
  • cross-post: stackoverflow.com/questions/38120500/… Commented Jun 30, 2016 at 14:20
  • @JeffSchaller Yes I thought I posted in the wrong forum and posted it here, but couldn't unfortunately delete it there Commented Jul 1, 2016 at 5:14

1 Answer 1

2

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"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.