环境:CentOS7
1.yum安装expect命令
#1.yum安装expect命令(也顺便会安装tcl命令,无需单独安装)
yum install expect -y 即可
2.编写shell脚本
#!/usr/bin/expect
set ip 192.168.0.105
set pass admin
set timeout 30
spawn ssh root@$ip
expect {
"(yes/no)" {send "yes\r"; exp_continue}
"password:" {send "$pass\r"}
}
expect "root@*" {send "df -h\r"}
expect "root@*" {send "exit\r"}
expect eof
3.执行
#3.执行(不要使用sh test.sh方式)
chmod +x test.sh
./test.sh