環境: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