title: Expect script: Create method to login to a remote server using ssh and send commands after method has returned
body: <p>I've been trying to write an expect script which has one method to open an ssh connection and this method can be called by other methods so that they can send additional commands for execution. What I already have is -</p>
  <pre><code>#!/usr/bin/expect
  
  set timeout -1
  #log_user 0
  set username [lindex ${argv} 0]
  set node_address [lindex ${argv} 1]
  set password [lindex ${argv} 2]
  set prompt &quot;.*\[&gt;#:$%\] \?\$&quot;
  set password_prompt &quot;.*: \?\$&quot;
  set install_dir &quot;/usr/share/elasticsearch&quot;
  set conf_dir &quot;/etc/elasticsearch&quot;
  set plugin_manager &quot;${install_dir}/bin/elasticsearch-plugin&quot;
  set es_config_file &quot;${conf_dir}/elasticsearch.yml&quot;
  
  puts &quot;Attempting login to ${node_address}&quot;
  spawn ssh ${username}@${node_address}
  expect -re ${password_prompt} {send -- &quot;${password}\n&quot;}
  expect -re ${prompt} {send -- &quot;sudo su - kompuser\n&quot;}
  expect -re ${prompt}
  puts &quot;Logged in to the node ${node_address} successfully&quot;}
  
  expect -re ${prompt}
  send -- &quot;curl -XGET 'localhost:9200/_cat/nodes?v&amp;h=ip,node.role'\n&quot;
  expect -re ${prompt}
  send_user $expect_out(buffer)
  </code></pre>
  <p>I have other scripts which try to login to a host using the similar code and have lots of code duplication. I want to avoid the code duplication by writing a single method, let's call it <code>login</code> and invoke it before sending any command to the remote server, what I want is -</p>
  <pre><code>#!/usr/bin/expect
  
  set timeout -1
  #log_user 0
  set username [lindex ${argv} 0]
  set node_address [lindex ${argv} 1]
  set password [lindex ${argv} 2]
  set prompt &quot;.*\[&gt;#:$%\] \?\$&quot;
  set password_prompt &quot;.*: \?\$&quot;
  set install_dir &quot;/usr/share/elasticsearch&quot;
  set conf_dir &quot;/etc/elasticsearch&quot;
  set plugin_manager &quot;${install_dir}/bin/elasticsearch-plugin&quot;
  set es_config_file &quot;${conf_dir}/elasticsearch.yml&quot;
  
  proc login {username node_address password prompt password_prompt} {
      puts &quot;Attempting login to ${node_address}&quot;
      spawn ssh ${username}@${node_address}
      expect -re ${password_prompt} {send -- &quot;${password}\n&quot;}
      expect -re ${prompt} {send -- &quot;sudo su - kompuser\n&quot;}
      puts &quot;Logged in to the node ${node_address} successfully&quot;}
  
  proc get_node_info {username node_address password prompt password_prompt} {
      login ${username} ${node_address} ${password} ${prompt} ${password_prompt}
      expect -re ${prompt}
      send -- &quot;curl -XGET 'localhost:9200/_cat/nodes?v&amp;h=ip,node.role'\n&quot;
      expect -re ${prompt}}
  
  proc restart_node {username node_address password prompt password_prompt} {
      login ${username} ${node_address} ${password} ${prompt} ${password_prompt}
      expect -re ${prompt} {send -- &quot;sudo systemctl daemon-reload\n&quot;}
      expect -re ${prompt} {send -- &quot;sudo systemctl start elasticsearch.service\n&quot;}
      expect -re ${prompt}
  }
  
  get_node_info ${username} ${node_address} ${password} ${prompt} ${password_prompt}
  restart_node ${username} ${node_address} ${password} ${prompt} ${password_prompt}
  </code></pre>
  <p>The issue is that call to login logs in successfully but expecting for the prompt in the caller hangs -
  Following are the last few lines of the logs on enabling the debug logs -</p>
  <pre><code>send: sending &quot;sudo su - kompuser\n&quot; to { exp4 }
  Logged in to the node 10.109.14.73 successfully
  Gate keeper glob pattern for '.*[&gt;#:$%] ?$' is ''. Not usable, disabling the performance booster.
  
  expect: does &quot;&quot; (spawn_id exp0) match regular expression &quot;.*[&gt;#:$%] ?$&quot;? (No Gate, RE only) gate=yes re=no
  </code></pre>
  <p>Any help will be much appreciated.
  Thanks</p>