title: Bash, Expect : Execution of command after SSH is not working
body: <p>I need to login to a VM and execute some command there. I have gone through all question related to the same topic but did not find any solution with EXPECT. I am using EXPECT as I need to pass the password while using SSH.</p>
  
  <p>I am getting "command not found error" while executing my script but manually, it is working fine.</p>
  
  <pre><code>#!/usr/bin/expect -f
  
  set user [lindex $argv 0]
  set to [lindex $argv 1]
  set pass [lindex $argv 2]
  set command [lindex $argv 3]
  puts "$user, $to , $command and $pass ."
  # connect via scp
  spawn sudo ssh -t -t -v $user@$to &lt;&lt; EOF
      ls
  EOF
  
  #######################
  expect {
  -re ".*es.*o.*" {
  exp_send "yes\r"
  exp_continue
  }
  -re ".*sword.*" {
  exp_send $pass\n
  }
  }
  interact
  </code></pre>
  
  <p>Error received :</p>
  
  <blockquote>
    <p>spawn sudo ssh -t -t -v username@server_ip &lt;&lt; EOF
    Invalid command name "ls"
        while executing "ls"
        (file "./establishSSHConnection.sh" line 10)</p>
  </blockquote>