9

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)?
Cœur
  • 37,241
  • 25
  • 195
  • 267

3 Answers3

11

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}
}
Kristofer
  • 3,201
  • 23
  • 28
  • 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 !!! –  Jan 11 '12 at 12:05
  • Anyway this is not good idea because my APP need to run fast without any delay from the expect script –  Jan 11 '12 at 12:19
  • I add the exp_continue string as you display here but its still stuck . –  Jan 11 '12 at 12:32
  • Note that the two strings are within the same expect{...} not as two separate expects as in your original script. – Kristofer Jan 11 '12 at 12:34
  • THX - yes now its work according to your example - its my mistake –  Jan 11 '12 at 12:53
  • 1
    -1 for the timeout suggesion, but +2 for `exp_continue` (which you don't want in the "password" block) – glenn jackman Jan 11 '12 at 13:48
  • 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 -:) –  Jan 11 '12 at 14:10
11

When you spawn SSH do this:

spawn ssh -o "StrictHostKeyChecking no" "$user\@ip"
Elmo
  • 6,409
  • 16
  • 72
  • 140
someguy
  • 111
  • 2
0

You may want to use the pexpect module for Python to do this, as it can provide you with something like a case :)

For more details, see: http://linux.byexamples.com/archives/346/python-how-to-access-ssh-with-pexpect/

Felix Yan
  • 14,841
  • 7
  • 48
  • 61
  • 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 –  Jan 11 '12 at 12:38