Reputation: 69
I am using expect to run a bash script which ssh's to 5 boxes, runs an aptitude dist-upgrade -d, zips up the packages, then scps them back to the original box.
All the boxes have the same password and all the boxes require an input of "y" to say yes to the dist-upgrade.
My current expect script is long and messy with loads of repeats of the following
## Original box
expect "password"
send "Password\n"
## Box sshing to
expect "password"
send "Password\n"
expect "want to continue?"
send "y\n"
My question is, how do I "dedupe" this so that where a password is asked for it sends the password, and where a continue prompt is asked for, it sends y. So that I don't have to get a perfect order of what will come when (sometimes a key check is thrown into the mix where I have to send "yes")
Full script below:
#!/usr/bin/expect -f
set timeout -1
## This uses manualpatching.sh, used to zip up a list of the latest Debian packages
spawn ./manualpatching.sh
## BOX1
expect "password"
send "Password\n"
expect "want to continue?"
send "y\n"
## BOX2
expect "password"
send "Password\n"
expect "password"
send "Password\n"
expect "want to continue?"
send "y\n"
expect "password"
send "Password\n"
expect "connecting"
send "yes\n"
## BOX3
expect "password"
send "Password\n"
expect "password"
send "Password\n"
expect "want to continue?"
send "y\n"
expect "password"
send "Password\n"
## BOX4
expect "password"
send "Password\n"
expect "password"
send "Password\n"
expect "want to continue?"
send "y\n"
expect "password"
send "Password\n"
## BOX5
expect "password"
send "Password\n"
expect "password"
send "Password\n"
expect "want to continue?"
send "y\n"
expect "password"
send "Password\n"
## BOX6
expect "password"
send "Password\n"
expect "password"
send "Password\n"
expect "want to continue?"
send "y\n"
expect "password"
send "Password\n"
## END
expect eof
Upvotes: 2
Views: 1499
Reputation: 69
Thanks to Gaurav for pointing me in the right direction.
I slimmed my script right down using exp_continue, it is now much more clean and versatile:
#!/usr/bin/expect -f
set timeout -1
spawn ./manualpatching.sh
expect {
"Are you sure you want to continue connecting (yes/no)" {
send "yes\r"
exp_continue
}
"password" {
send "Password\r"
exp_continue
}
"want to continue?" {
send "y\r"
exp_continue
}
}
Upvotes: 4