1.安装expect
yum install -y expect
2.expect原理
[root@localhost ~]
/usr/bin/expect
set timeout 30
spawn ssh root@192.168.2.105
expect {
"yes/no" {send "yes\r";exp_continue}
"password" {send "123456\r"}
}
interact
[root@localhost ~]
spawn ssh root@192.168.2.105
root@192.168.2.105's password:
Last login: Thu Mar 19 14:13:32 2020 from 192.168.2.107
expect_tmp() {
expect -c "
set timeout 10
spawn cmd ...
expect {
"[Y/n]" {
send "Y\n"
}
}
interact
"
echo expect_tmp done!!!
}
expect_tmp
echo Done!!!
exit 0