Expect script + how to ignore lines if not appearing

I am writing the following expected script to automate ssh login to a remote Linux machine And run the command "cat / etc / APP_VERSION.txt file"

I once did not ask from the ssh team about -

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

So, in this case, the first wait is still waiting for the string "(yes / no)?" - (

And do not continue the second wait - "password:"?

How to resolve this ?,

I mean, if the line "Are you sure you want to continue the connection (yes / no)?" Is not appears from ssh

Then we should move on to the next wait for "password:", but how to do it?

note1 - in the case of the question "Do you really want to continue the connection (yes / no)?" appears, then waiting for the script works fine without problems.

note2 - I cannot use a positive timeout value (like timeout 10) because ssh itself has a delay

. ,,.

My wait script: (this wait 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 . . . 

. ,,,.

ssh example 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)? 
+6
source share
3 answers

Set timeout

set timeout 10

Waiting for the expected line for 10 seconds, and then moving to the next line in the script if it is not received within the specified timeout.

Link 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 can try using a list of alternative answers to jump forward the script.

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

When you create SSH, follow these steps:

 spawn ssh -o "StrictHostKeyChecking no" "$user\@ip" 
+7
source

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

See http://linux.byexamples.com/archives/346/python-how-to-access-ssh-with-pexpect/ for more details.

0
source

Source: https://habr.com/ru/post/905704/


All Articles