title: scripting sftp with expect
body: <p>I have to script an sftp-access with password-authentification. Unfortunately our customer can't create a key-authentification on the server we have to access.</p>
  
  <p>All the sftp-Commands are generated and written in a temporary file (tmp.txt). Following command works fine on the command-line:</p>
  
  <pre><code>sftp OnlineRegTest_BS@ftp.b-n-c.ch &lt; tmp.txt
  </code></pre>
  
  <p>The problems starts when I'd like to use "expect" because I have to script it and use a password. </p>
  
  <pre><code>EXPECTSCRIPT=`cat &lt;&lt;EOF
      spawn sftp user@server &lt; tmp.txt
      expect "password"
      send ${pass}
      send "\r" 
  EOF`
  expect -c "$EXPECTSCRIPT"
  </code></pre>
  
  <p>If I remove the "&lt; tmp.txt" the connection will establish. But I need the commands from the tmp.txt. Here's the error-Message:</p>
  
  <pre><code>[root@xxx web]# ./transfer.sh
  spawn sftp user@server &lt; tmp.txt
  usage: sftp [-1Cv] [-B buffer_size] [-b batchfile] [-F ssh_config]
              [-o ssh_option] [-P sftp_server_path] [-R num_requests]
              [-S program] [-s subsystem | sftp_server] host
         sftp [[user@]host[:file [file]]]
         sftp [[user@]host[:dir[/]]]
         sftp -b batchfile [user@]host
  send: spawn id exp6 not open
      while executing
  "send password"
  </code></pre>
  
  <p>Does someone have any ideas?</p>
  
  <p>I also couldn't get it running with sftp -b... :-(</p>
  
  <p><em>edit for @glenn jackman</em></p>
  
  <pre><code>EXPECTSCRIPT2=`cat &lt;&lt;EOF
          spawn sh -c {sftp user@server &lt; tmp.txt}
          expect "password"
          send ${pass}
          send "\r"
          expect "sftp&gt;"
          expect "sftp&gt;"
          .....
          expect "sftp&gt;"
          expect "sftp&gt;"
          expect "sftp&gt;"
  EOF`
  </code></pre>
  
  <p>the tmp.txt looks like this:</p>
  
  <pre><code>cd d
  put d/d1
  cd ../e
  put e/e1
  put e/e2
  cd ../f
  put f/f1
  </code></pre>