title: Using 'expect' command to pass password to SSH running script remotely
body: <p>I need to create a bash script that will remotely run another script on a batch of machines.  To do so I am passing a script through SSH.</p>
  
  <pre><code>ssh -p$port root@$ip 'bash -s' &lt; /path/to/script/test.sh
  </code></pre>
  
  <p>I thought it would use my RSA keys but I am getting error:</p>
  
  <pre><code>"Enter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)"
  </code></pre>
  
  <p>I tried using sshpass to no avail.  So my next solution was using expect. I have never used expect before and I'm positive my syntax is way off.</p>
  
  <pre><code>ssh -p$port root@$ip 'bash -s' &lt; /path/to/script/test.sh
  /usr/bin/expect &lt;&lt;EOD
  expect "password"
  send "$spass\n"
  send "\n"
  EOD
  </code></pre>
  
  <p>I have root access to all machines and ANY solution will do as long as the code remains within bash.  Just keep in mind that this will be done in a loop with global variables ($spass, $ip, $port, etc) passed from a parent script.</p>