expect tool can be installed with command:
sudo apt-get install expect
Expect tool has own commands that can be used in its scripts, here is the list of the ones that are essential:
spawn - to execute another program
expect - to wait for text on the screen
send - simulate typing on keyboard, e.g. typing password
interact - leave the expect mode and allow user to continue work
Expect tool can be used with input file, and it ca be called like this:
expect -f script_name
Here is the example of script for remote login:
set user "your_user_name"
set pass "secret"
spawn ssh ${user}@${server}
expect ": $"
send "$pass\n"
interact
And here is the example of script for secure copy of several files to a remove machine that are in different directories:
set timeout 1200
set src "/home/user/workspace/"
set user "your_user_name"
set pass "secret"
set paths {path1/file1.war path2/file2.war}
set server 192.168.0.100
set spath :~/
foreach {p} $paths {
spawn scp ${src}/${p} ${user}@${server}${spath}
expect ": $" {
send "$pass\n"
}
expect ":~"
puts "\r\n"
}
