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

 

posted on 2019-04-23 16:21  北溟有鱼。  阅读(3858)  评论(0编辑  收藏  举报