#!/bin/bash ips=( '127.0.0.1' ) for(( i=0;i<${#ips[*]};i++)) do expect <<EOF #这里的 expect <<EOF是指 在输入EOF时结束,expect <<-EOF,前面的-只是tab,不是空格,对应底下的EOF前面缩进还是顶格 set timeout 3 spawn ssh intfish@${ips[i]} expect { "*yes/no" { send "yes " } "*password:" { send "root " } } expect -re ".*[$#]" send "df -h " expect -re ".*[$#]" send "exit " expect eof EOF done