title: while loop issues in expect script
body: <p>I have written a expect script which works as follows:</p>
  
  <ol>
  <li>ssh to server1 </li>
  <li>From server1 ssh to another server server2</li>
  <li>From server2 to server3 and then sudo into a user and run the commands.</li>
  </ol>
  
  <p>In the script I read the hostnames and commands to be executed from two files called hostnames.out and commands.out. I used a while loop to iterate through each entries in hostnames.out and run the commands from the commands.out file. </p>
  
  <p>I tested my script with single entry in the hostnames.out it works fine, but when i add multiple lines it is not running the commands on the hostnames from the second line onwards.</p>
  
  <p>The format of the commands.out file is(one command per line):</p>
  
  <pre>
  ls -lrt
  hostname
  whoami
  </pre>
  
  <p>The format of the hostnames.out file is:</p>
  
  <pre>
  server1 user password server2 user password server3 user password
  </pre>
  
  <p>I have attached the script for reference. Please let me know where the problem is.</p>
  
  <pre><code>#!/usr/bin/expect
  #####################################################
  # script to automate manual works - remote 2        #
  # Gopinath                                          #
  #####################################################
  #Variable declaration:
  
  #Setting variable "prompt" for multiple prompts:
  set prompt {[]$#%]\s*$}
  
  #Reading commands list from file:
  
  set fp1 [open "commands_list_2.out" "r"]
  set file_data [read $fp1]
  close $fp1
  
  
  #  read the hosts file one line at a time
  #  There should be no new line at the end of the hostnames.out file
  
       set fp [open "hostnames_2.out" "r"]
  
       while { [gets $fp data] &gt;= 0 } {
  
            set ssh1 [lindex $data 0]
            set ssh1_usr [lindex $data 1]
            set ssh1_pwd [lindex $data 2]
        set ods [lindex $data 3]
        set ods_usr [lindex $data 4]
            set ods_pwd [lindex $data 5]
        set serv1 [lindex $data 6]
        set serv1_usr [lindex $data 7]
        set serv1_pwd  [lindex $data 8]
  
            puts $ssh1 
        puts $ssh1_usr
            puts $ssh1_pwd
        puts $ods
        puts $ods_usr
        puts $ods_pwd
        puts $serv1
        puts $serv1_usr
            puts $serv1_pwd
  
       spawn -noecho ssh $ssh1_usr@$ssh1
       expect { 
  
          "*password:" { send "$ssh1_pwd\r"}
          "*route*" { puts "login failed"; exit 1 }
          "timed out" { puts "login failed timed out"; exit 1 }
           }
  
        expect {
                 -re $prompt { send "whoami\r"}
               }            
  
  
                    expect -re $prompt  { 
  
                              send  "ssh $ods_usr@$ods\r" }           
                    expect {
                       "password:" { send "$ods_pwd\r" }
                           }
                   }
  
  
             expect {
           -re $prompt { send "whoami\r"}
            }
  
  
                     expect -re $prompt  {
  
                              send  "ssh $serv1_usr@$serv1\r" }
                    expect {
                       "password:" { send "$serv1_pwd\r" }
                           }
  
                     expect -re $prompt 
  
  
           foreach a [list $file_data] {
           send "$a"
           expect -re prompt
           }
            expect -re prompt {   
                send "exit\r"
                }
       expect eof
       close $fp
  `
  </code></pre>
  
  <hr>