expect

#!/usr/bin/expect -f
set ipaddr "192.168.5.4"
set passwd "123qwe"
set timeout 30

spawn ssh root@$ipaddr
expect {
"yes/no" { send "yes "; exp_continue }
"password: " { send "$passwd " }
}
expect "*# "
send "ls "
send "exit "
expect eof
exit

原文地址:https://www.cnblogs.com/hygs/p/5677963.html