linux通过expect远程登录并执行命令

#!/usr/bin/expect
set timeout 60
set username root
set password 88888888
set hostname syd

spawn ssh $username@$hostname
expect {
"yes/no"
{send "yes "; exp_continue;}
"password:"
{send "$password ";}
}
expect -re "root@Sunyard" {send "touch test.txt "}
expect -re "root@Sunyard" {send "exit "}
exit

原文地址:https://www.cnblogs.com/timlong/p/14681479.html