title: getting error "expect: spawn id exp4 not open" while trying to grep
body: <p>Im trying to compile a script that establishes an ssh connecting with a remote machine and searches multiple log files for error messages, my current code is the following:</p>
  
  <pre><code>#!/bin/bash
  
  expect &lt;&lt;-EOF &gt; /home/file
  set timeout 3
  spawn ssh -oPort=port ip zgrep "error" /dir/dir/file.gz.\[54321\]
  expect "Are you sure you want to continue connecting (yes/no)?" { send "yes\r" }
  expect "*password: " { send "password\r" }
  expect "*#" { send "exit\r" }
  EOF
  expect &lt;&lt;-EOF &gt;&gt; /home/file
  set timeout 3
  spawn ssh -oPort=port ip zgrep "error" /dir/dir/file1.gz.\[54321\]
  expect "Are you sure you want to continue connecting (yes/no)?" { send "yes\r" }
  expect "*password: " { send "password\r" }
  expect "*#" { send "exit\r" }
  EOF
  expect &lt;&lt;-EOF &gt;&gt; /home/file
  set timeout 3 ip
  spawn ssh -oPort=port ip  grep error /dir/dir/file
  expect "Are you sure you want to continue connecting (yes/no)?" { send "yes\r" }
  expect "*password: " { send "password\r" }
  expect "*#" { send "exit\r" }
  EOF
  expect &lt;&lt;-EOF &gt;&gt; /home/file
  set timeout 3
  spawn ssh -oPort=port ip grep error /dir/dir/file1
  expect "Are you sure you want to continue connecting (yes/no)?" { send "yes\r" }
  expect "*password: " { send "password\r" }
  expect "*#" { send "exit\r" }
  EOF
  </code></pre>
  
  <p>Now have you may have noticed i connect to the machine 4 times, which feels so inefficient, i have tried to put all of this log searches into 1 ssh connection, like the following:</p>
  
  <pre><code>expect &lt;&lt;-EOF &gt; /home/file
  set timeout 3
  spawn ssh -oPort=port ip zgrep "error" /dir/dir/file.gz.\[54321\]
  expect "Are you sure you want to continue connecting (yes/no)?" { send "yes\r" }
  expect "*password: " { send "password\r" }
  expect "*#" { send "grep error /dir/dir/file1\r" }
  expect "*#" { send "exit\r" }
  EOF
  </code></pre>
  
  <p>but when i try this i get the error</p>
  
  <pre><code>spawn id exp4 not open
      while executing
  "expect "*#" { send "grep error /dir/dir/file1\r" }"
  </code></pre>
  
  <p>What is wrong with this code that make it output this error,
  All help appreciated</p>