w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Using expect in Perl with system()

Add a yes/no match to the same invocation of expect as the password match:

expect '*yes/no*' {send "yes
"; exp_continue;} '*?assword:*' {send "$password
"};

This will look for both matches, if yes/no is encountered exp_continue tells expect to keep looking for the password prompt.

Full example:

system( qq{expect -c 'spawn  ssh  $username@$ip; expect
'*yes/no*' {send "yes
"; exp_continue;} '*?assword:*' {send "$password
"}; expect '*?*' {send "mkdir -p
~/$remote_start_folder/$remote_folder_name/$remote_username/$remote_date/
"}; expect '*?*' {send "exit
"};  interact;'} );

I've also used qq to avoid having to escape all the quotation. Running this command from a shell with -d flag shows expect looking for either match:

Password: 
expect: does "...

Password: " (spawn_id exp4) match glob pattern
    "*yes/no*"? no
    "*?assword:*"? yes

With yes/no prompt:

expect: does "...continue connecting (yes/no)? " (spawn_id exp4)
match glob pattern
    "*yes/no*"? yes
...
send: sending "yes
" to { exp4 }
expect: continuing expect
...
expect: does "...
Password: " (spawn_id exp4) match glob pattern
    "*yes/no*"? no
    "*?assword:*"? yes
...
send: sending "password
" to { exp4 }




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.