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
相關文章
相關標籤/搜索