Charmander
Charmander

Reputation: 119

expect commands terminate too early

I'm trying to write a script to automatically log into a server, run a few lines of command(to install Anaconda), and then exit. The script below executes nicely until the line this, which starts off fine but ends abruptly (after installing only 10 or so libraries) without giving any error message. Is it because a timeout setting for the expect script? Any ideas on how to fix it?

#!/usr/bin/expect

set f [open "/Users/user1/Downloads/servers.txt"]
set hosts [split [read $f] "\n"]
close $f

set f [open "commands.txt"]
set commands [split [read $f] "\n"]
close $f

foreach host $hosts {
     spawn ssh -o StrictHostKeyChecking=no USERNAME@$host;
     expect "?assword:"
     send "PASSWORD\r"

     foreach cmd $commands {
        expect "$ "
        send "$cmd\r"
     }

     expect "$ "
     send "exit\r"

}

where servers.txt is just a list of servers and commands.txt is the following:

wget https://repo.anaconda.com/archive/Anaconda3-5.2.0-Linux-x86_64.sh -O ~/anaconda.sh
bash ~/anaconda.sh -b -p $HOME/anaconda
echo 'export PATH="$HOME/anaconda/bin:$PATH"' >>~/.bash_profile
source .bash_profile

Upvotes: 1

Views: 671

Answers (1)

Charmander
Charmander

Reputation: 119

The expect script set timeout = 10 automatically. So you need to change the timeout at the beginning of the script using set timeout 120 or whatever time.

Upvotes: 5

Related Questions