title: Expect ssh script (execute command on remote machine)
body: <p>I'm writing an expect script that executes a command on a remote server using ssh.</p>
  
  <p>Command syntax: <code>ssh &lt;classname&gt; &lt;command&gt;</code></p>
  
  <p>Code:</p>
  
  <pre><code>set ip "hive28.cs.berkeley.edu"
  set class [lindex $argv 0]
  // set $user and $ip according to class
  
  set cmd [lindex $argv 1]
  spawn ssh "$user\@$ip '$cmd'"
  expect "assword"
  send "$password\r";
  interact
  </code></pre>
  
  <p>Unfortunately, I get this error:</p>
  
  <pre><code>~/foo&gt; ssh2 162 'pwd'
  
  spawn ssh ***@hive28.cs.berkeley.edu 'pwd'
  ssh: Could not resolve hostname hive28.cs.berkeley.edu 'pwd': Name or service not known
  send: spawn id exp6 not open
      while executing
  "send "$password\r""
      invoked from within &lt;...&gt;
  </code></pre>
  
  <p>But when I run the generated command directly, it works (ignore the <code>gdircolors</code> warnings):</p>
  
  <pre><code>~/foo&gt; ssh ***@hive28.cs.berkeley.edu 'pwd'
  ***@hive28.cs.berkeley.edu's password: 
  /home/ff/cs162/adm/bashrc.d/70-gnu.sh: line 36: gdircolors: command not found
  /home/cc/cs162/sp16/class/***
  </code></pre>