最简单的非交互ssh远程执行命令expect脚本

标题有点绕口~

#!/usr/bin/expect -f

if { $argc<4 } {
    puts stderr "Usage: $argv0 <host> <user> <passwd> <cmd>"
    exit 1   
}

set HOST [lindex $argv 0]
set USER [lindex $argv 1]
set PSWD [lindex $argv 2]
set CMDS [lindex $argv 3]

log_user 0
set timeout 3
spawn -noecho ssh $USER@$HOST "$CMDS"
while {1} {
    expect {
        "(yes/no)? " { send "yes\r"; continue; }
        "Password: " { send "$PSWD\r"; log_user 1 }
        eof { break }
        timeout { send_user "\[timeout\]\n"; exit 255 }
    }
}
原文地址:https://www.cnblogs.com/custa/p/2960602.html