title: Expect Script Not Working for Automatic Server Shutdown
body: <p>Let me preface this question by saying I am VERY new to using tools like Expect, so the solution to this may be rather simple and obvious.</p>
  
  <p>We are trying to automate the shutdown of our WebSphere server on our AS400 for a specific process that we have to run monthly. This happens by calling a CL program that runs a QSH session from a statement file. Actually calling the expect program seems to work fine.</p>
  
  <p>The issue we run into is that it seems whatever sort of internal variable (presumably exp0) the expect is using to compare the expected text to isn't actually being attributed a new value when new commands get ran. Here is code in the statement file that gets ran:</p>
  
  <pre><code>#!/usr/local/bin/expect -f                                    
  set timeout 320                                               
  exp_internal 1                                                
  
  send "cd /qibm/proddata/websphere/appserver/v85/express/bin\r"
  expect "$"                                                    
  
  send "stopServer WAS85SVR\r"                                  
  expect {                                                      
  default {exit 2}                                              
  "Username:" {send "username\r"; exp_continue}                 
  "Password:" {send "password\r"; exp_continue}                 
  "$"                                                           
  }                                                             
  
  expect "$"     
  </code></pre>
  
  <p>The sends in the expect that say "username" and "password" would normally contain actual username and password text. Here is what gets dumped into the log file for the run:</p>
  
  <pre><code>send: sending "cd /qibm/proddata/websphere/appserver/v85/express/bin\r" to { exp0 cd /qibm/proddata/websphere/appserver/v85/express/bin
  }
  
   expect: does "" (spawn_id exp0) match glob pattern "$"? yes        
   expect: set expect_out(0,string) ""                                
   expect: set expect_out(spawn_id) "exp0"                            
   expect: set expect_out(buffer) ""                                  
   send: sending "stopServer WAS85SVR\r" to { exp0 stopServer WAS85SVR
  }                                                                   
  
   expect: does "" (spawn_id exp0) match glob pattern "Username:"? no 
   "Password:"? no                                                    
   "$"? yes                                                           
   expect: set expect_out(0,string) ""                                
   expect: set expect_out(spawn_id) "exp0"                            
   expect: set expect_out(buffer) ""                                  
  
   expect: does "" (spawn_id exp0) match glob pattern "$"? yes        
   expect: set expect_out(0,string) ""    
   expect: set expect_out(spawn_id) "exp0"
   expect: set expect_out(buffer) ""      
  </code></pre>
  
  <p>The "\r"s have been included just for log file readability and I don't believe they end up translating into the sends that are passed. Also, it may be worth noting that the "stopServer" command runs for about 10 seconds before the "Username:" prompt shows up. We've tried putting in a delay prior to expecting for "Username:", but that didn't seem to work either.</p>
  
  <p>If it is worth seeing, here is what a manual/interactive session in QSH looks like up to the "Username:" prompt:</p>
  
  <pre><code>  $                                                                                                       
  &gt; cd /qibm/proddata/websphere/appserver/v85/express/bin                                                   
    $                                                                                                       
  &gt; stopServer WAS85SVR                                                                                     
    ADMU0116I: Tool information is being logged in file /QIBM/UserData/WebSphere/AppServer/V85/Express/profiles/WAS85SVR/logs/WAS85SVR/stopServer.log
    ADMU0128I: Starting tool with the WAS85SVR profile                                                      
    ADMU3100I: Reading configuration for server: WAS85SVR                                                   
    Realm/Cell Name: &lt;default&gt;                                                                              
    Username:                                                                                               
  </code></pre>