Redirection expect stdin to call

Is there a way to redirect stdin sent to expect it to be sent to a spawn call while waiting? In my example below, I am assuming an embedding in a shell function to which I want to connect another shell script through heredoc and suppress the output by translating it into a shell variable.

    psshstdin() {
      local user=$1 pass=$2 hosts=$3
      out=$(expect -c '
      set timeout 15
      spawn pssh -i -h '"$hosts"' -p 100 -l '"$user"' -A -o ./ -x-oStrictHostKeyChecking=no <EXPECT_STDIN_HERE
      expect "assword:" { send '\""$pass\r\""' }
      interact
    '<<EOF
    echo "hello"
    echo "world"
    EOF
    )
    }

SOLUTION: I had to post this here because I do not have enough reputation points to answer my own question so quickly.

I was able to solve this by trying the same methods as in this problem . I did not think that the solution was applicable initially, but it was so. The working code is shown below.

psshstdin() {
  local user=$1 pass=$2 hosts=$3
out=$(expect -c '
set timeout 30
spawn pssh -I -h '"$hosts"' -p 100 -l '"$user"' -A -o ./ -x-oStrictHostKeyChecking=no
while {[gets stdin line] != -1} {
  send "$line\n"
}
send \004
expect "assword:" { send '\""$pass\r\""' }
expect {
  "END_TOKEN_OF_SCRIPT" {
    exit 0
  }
  default {
    exit 1
  }
}'<&0)
}

I can call it something like:

psshstdin myusername mypassword ssh_hosts_file<<EOF
echo "hello"
echo "world"
EOF
+3
source share
1

stdin stdin=$(cat -)

: stdin:

untested, :

psshstdin() {
  local user=$1 pass=$2 hosts=$3
  out=$(expect -c '
    set stdin [read stdin]
    puts "debug: sending the following stdin to pssh:\n$stdin\n--END--"
    set timeout 15
    spawn echo "$stdin" | pssh -i -h '"$hosts"' -p 100 -l '"$user"' -A -o ./ -x-oStrictHostKeyChecking=no
    expect "assword:" { send "'"$pass"'\r" }
    interact
  '<<EOF
echo "hello"
echo "world"
EOF
)
}
0

All Articles