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

猜你喜欢

转载自www.cnblogs.com/f-society/p/10757135.html