Search code examples
rsyncexpect

Expect scripts need a focused window session to work?


I have the following expect script to sync a local folder with a remote one:

    #!/usr/bin/expect -f
    # Expect script to interact with password based commands. It synchronize a local 
    # folder with an remote in both directions.
    # This script needs 5 argument to work:
    # password = Password of remote UNIX server, for root user.
    # user_ip = user@server format
    # dir1=directory in remote server with / final
    # dir2=local directory with / final
    # target=target directory
    # set Variables
    set password [lrange $argv 0 0] 
    set user_ip [lrange $argv 1 1]   
    set dir1 [lrange $argv 2 2]   
    set dir2 [lrange $argv 3 3]   
    set target [lrange $argv 4 4]   
    set timeout 10   
    # now connect to remote UNIX box (ipaddr) with given script to execute
    spawn rsync -ruvzt -e ssh $user_ip:$dir1$target $dir2
    match_max 100000
    expect {
    -re ".*Are.*.*yes.*no.*" {
    send "yes\n"
    exp_continue
    }
    # Look for password prompt
    "*?assword*" {
    # Send password aka $password 
    send -- "$password\r"
    # send blank line (\r) to make sure we get back to gui
    send -- "\r"
    interact
    }
    }
    spawn rsync -ruvzt -e ssh $dir2$target $user_ip:$dir1
    match_max 100000
    expect {
    -re ".*Are.*.*yes.*no.*" {
    send "yes\n"
    exp_continue
    }
    # Look for password prompt
    "*?assword*" {
    # Send password aka $password 
    send -- "$password\r"
    # send blank line (\r) to make sure we get back to gui
    send -- "\r"
    interact
    }
    }
    spawn ssh $user_ip /home/pi/bash/cerca_del.sh $dir1$target
    match_max 100000
    expect {
    -re ".*Are.*.*yes.*no.*" {
    send "yes\n"
    exp_continue
    }
    # Look for passwod prompt
    "*?assword*" {
    # Send password aka $password 
    send -- "$password\r"
    # send blank line (\r) to make sure we get back to gui
    send -- "\r"
    interact
    }
    }

It work properly if I execute it in a gnome_terminal window, but it stops to the password request if I execute in foreground (such us using ALT+F2 combination, or with crone, or with a startup script).

I don't found information if expect needs of an active windows terminal to interact correctly.

Somebody else experiments this strange behaviour? It is a feature or a bug? Any solution?

Thank you.


Solution

  • As I commented to Glenn's answer, I saw that the problem wasn't the terminal windows but the way the script is called. My expect script is called several times by another BASH script with the rude line: "/path/expect-script-name.exp [parameters]". Opening a terminal window (in any desktop environment), I can execute the caller script by: "/path/bash-script-name.sh". In this way, everything run well because the shebang is used to call the right shell (in this case EXPECT). I added in the start-up system list the BASH script (i.e. the caller script of the EXPECT script) working in a non-focused terminal window instance. This last way gives errors.

    The solution is calling explicitly the EXPECT script in the BASH script in the way: "expect /path/expect-script-name.exp".

    I found that without this explicit call the shell DASH manages all the scripts (included the EXPECT scripts).