title: Expect Tcl script - Error passing quoted argument using spawn
body: <p>I just wrote a very simple Expect script for wrapping around rsync, but it seems to be giving me trouble.  Basically, I am automating the SSH login prompt called from rsync.  I also have to pass arguments through rsync to SSH so it doesn't do the host key checking.  I am well aware of SSH authentication keys and ssh-keygen, but I have good reasons for doing things this way so no lectures on passing passwords on the command-line.</p>
  
  <h1>Script</h1>
  
  <hr>
  
  <pre><code>#!/usr/local/bin/expect -f
  
  if {$argc != 5} {
    puts "usage: remoteCopy {remotehost, username, password, localfile, remoteloc}"
    exit 1
  }
  
  set remotehost [lindex $argv 0]
  set username [lindex $argv 1]
  set password [lindex $argv 2]
  set localfile [lindex $argv 3]
  set remoteloc [lindex $argv 4]
  
  set timeout -1
  
  spawn rsync -e \"ssh -q -o StrictHostKeyChecking=no\" $localfile $username@$remotehost:$remoteloc
  expect "Password"; send "$password\r"
  </code></pre>
  
  <p>Here is the complete output from the script:</p>
  
  <h1>Output</h1>
  
  <hr>
  
  <pre><code>avoelker@localhost  $ ./remoteFileCopy.tcl remotehost remoteuser remotepass ~/localfile /tmp/
  spawn rsync -e "ssh -q -o StrictHostKeyChecking=no" /localhost/home/avoelker/localfile remoteuser@remotehost:/tmp/
  rsync: Failed to exec "ssh: No such file or directory (2)
  rsync error: error in IPC code (code 14) at pipe.c(83)
  rsync: connection unexpectedly closed (0 bytes received so far) [sender]
  rsync error: error in rsync protocol data stream (code 12) at io.c(434)
  send: spawn id exp6 not open
      while executing
  "send "$password\r""
      (file "./remoteFileCopy.tcl" line 17)
  avoelker@localhost  $
  </code></pre>
  
  <p>It appears to me that rsync is trying to execute <code>"ssh</code> instead of just <code>ssh</code>, however, copying and pasting the first line of output from Expect's stdout (<code>rsync -e "ssh -q -o StrictHostKeyChecking=no" /localhost/home/avoelker/localfile remoteuser@remotehost:/tmp/</code>) directly into the shell works perfectly fine, so I know it isn't a problem with rsync.</p>
  
  <p>When I remove the -e argument entirely from the <code>spawn rsync</code> line, the Expect script executes without errors, but I would have to add in host key checking to the script, which I do not want to do.</p>
  
  <p>Does someone more experienced with Expect/Tcl know what I am doing wrong?</p>