shell 结合expect实现ssh登录并执行命令
#!/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\r" } "*password:" { send "root\r" } } expect -re ".*\[\$#\]" send "df -h\r" expect -re ".*\[\$#\]" send "exit\r" expect eof EOF done
人生如修仙,岂是一日间。何时登临顶,上善若水前。