expect语言用来实现自动和交互式任务进行通信,而无需人的干预。 #!/usr/bin/expect //告诉操作系统脚本里的代码使用那一个shell来执行 set ip 192.168.1.156 //变量赋值 set pass 123.com
set timeout 30 //设置超时时间 spawn ssh root@$ip //给ssh运行用来传递交互指令 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