expect script + how to ignore strings if not appears

18,079

Solution 1

Set a timeout

set timeout 10

Will wait for the expected line for 10 seconds, and then move on to the next line in the script if not received within the specified timeout.

Reference http://linuxshellaccount.blogspot.com/2008/01/taking-look-at-expect-on-linux-and-unix.html

UPDATE: If a timeout is not an acceptable solution you could try using a list of alternative responses to drive the script forward.

expect {
 "(Are you sure you want to continue connecting yes/no)?" { send "yes\r"; exp_continue }
 password:                                                {send PASS123\r; exp_continue}
}

Solution 2

When you spawn SSH do this:

spawn ssh -o "StrictHostKeyChecking no" "$user\@ip"
Share:
18,079
Admin
Author by

Admin

Updated on July 29, 2022

Comments

  • Admin
    Admin almost 2 years

    I write the following expect script in order to automate ssh login to remote Linux machine And run the command "cat /etc/APP_VERSION.txt file"

    Sometime I not asked from ssh command about-

        "Are you sure you want to continue connecting (yes/no)?"
    

    So in this case the first expect still wait for the string "(yes/no)?" -:(

    And not continue to the second expect - "password:" ?

    How to resolve this? ,

    I mean if the line "Are you sure you want to continue connecting (yes/no)?" Isn’t appears from ssh

    Then we must go to the next expect "password:" , but how to do this?

    remark1 - in case the question "Are you sure you want to continue connecting (yes/no)?" , Appears then the expect script work fine without any problem .

    remark2 - I can’t use timeout positive value (as timeout 10) because ssh itself have delay

    . . . .

    My expect script: (this expect is part of my ksh script)

      .
      .
      .
    
      APP_MACHINE=172.12.6.190  
    
    
      set timeout -1
      spawn  ssh $APP_MACHINE  
           expect {
                    "(Are you sure you want to continue connecting yes/no)?" 
                                  { send "yes\r" }
                  }
           expect password:        {send PASS123\r}
         expect >  {send "cat /etc/APP_VERSION.txt\r"}
       expect >    {send exit\r}
      expect eof
    
     .
     .
     .
    

    . . . . .

    example of ssh from my linux machine

      ssh APP_MACHINE
      The authenticity of host 'APP_MACHINE (172.12.6.190 )' can't be established.
      RSA key fingerprint is 1e:8a:3d:b3:e2:8c:f6:d6:1b:16:65:64:23:95:19:7b.
      Are you sure you want to continue connecting (yes/no)?
    
  • Admin
    Admin over 12 years
    Sorry I can’t used it because some time ssh have itself have time connection – sometimes its 5 or 10 seconds who know? - Its isn’t good solution for me sorry again , its must be other solution !!!
  • Admin
    Admin over 12 years
    Anyway this is not good idea because my APP need to run fast without any delay from the expect script
  • Admin
    Admin over 12 years
    I add the exp_continue string as you display here but its still stuck .
  • Kristofer
    Kristofer over 12 years
    Note that the two strings are within the same expect{...} not as two separate expects as in your original script.
  • Admin
    Admin over 12 years
    thx felix sun , but I prefer first to solve it in the expect because I have allot expect script in my shell script so I not want to spend allot time to translate it to pylon but your idea is good for new scratch cod
  • Admin
    Admin over 12 years
    THX - yes now its work according to your example - its my mistake
  • glenn jackman
    glenn jackman over 12 years
    -1 for the timeout suggesion, but +2 for exp_continue (which you don't want in the "password" block)
  • Admin
    Admin over 12 years
    YES I agree with you , I wait for along time to this solution , if you think I have agood question , you can upvote for me also -:)