title: expect - ssh - two possible passwords - or how to jump of expect block and still use send clausule
body: <p>first I need to say that I am avare of using public and private keys but this did not solve my problem. Also I am aware that I can use two scripts (one with password1 and second with password2) and run them two times but I am wondering why following did not works. I am trying to make expect script that will connect through ssh and check if one of two passwords are correct. For this I am checking exit codes of script:</p>
  
  <ul>
  <li>0 - password1 was correct</li>
  <li>1 - password2 was correct</li>
  <li>2,3,4,5,6 - some other exit codes, insipred by this post: <a href="https://stackoverflow.com/questions/13705020/linux-expect-send-weird-constructions">expect send weird
  constructions</a></li>
  </ul>
  
  <p>There can be more possible login prompt on remote machine and I would like to make this script as generic as possible, so I cannot rely on the login prompt string. So far I have this code:</p>
  
  <pre><code>#!/usr/local/bin/expect
  set username "username";
  set remote_server "machine";
  set password1 "badpassword";
  set password2 "goodpassword";
  
  set timeout 5;
  set pretype 0;
  set retcode 0;
  
  
  if { $pretype == 0 } {
    spawn ssh -q ${username}@${remote_server}
    expect {
      "no)?"    { send "yes\r"; }
      "denied" {
                  puts "Can't login to $remote_server. Check username and password\n";
                  set retcode 2;
               }
      "telnet:" {
                  puts "Can't connect to $remote_server via SSH or Telnet. Something went definitely wrong\n";
                  set retcode 3;
                }
      "failed" {
                  puts "Host $remote_server exists. Check ssh_hosts file\n";
                  set retcode 4;
               }
      timeout {
                  puts "Timeout problem. Host $remote_server doesn't respond\n";
                  set retcode 5;
              }
      "refused" {
                  puts "Host $remote_server refused to SSH. That is insecure.\n";
                  set retcode 6;
                  }
      "assword:" {    set pretype 1; send "${password1}\r";  }
    }
  } else {
      expect{
          "assword:" {    send "${password2}\r"; exit 1; }
          timeout { exit 0; }
      }
  }
  </code></pre>
  
  <p>The idea behind this is following: if I type wrong password I will have second opportunity to type correct one. So If I type wrong password first time my script should continue in else branch (because I <code>set pretype to 1</code>). Here I will expecting prompt for entering password again. So I will send second password and exit with code 1 (password2 was OK). If the timeout in else branch expires (the "password:" string did not match), most probably I already have login prompt, so script will exit with code 0 (password1 was OK). But this did not working, and if I ran this inside debugger the last line I saw was:</p>
  
  <pre><code>send: sending "badpassword\r\r" to { exp4 }
  </code></pre>
  
  <p>I do not know why this happens, but seems that script never reaches the else branch. Thank you very much</p>