我可以在期望的远程SSH主机上执行命令。只要我将自己限制为单行硬编码命令,一切都会好起来。但是,我想创建一个本地命令脚本以远程执行。
此方法有效,但只能使用一行命令:
#!/usr/bin/expect
set USER [lindex $argv 0]
set PASSWORD [lindex $argv 1]
set CMD [lindex $argv 2]
set timeout 10
spawn ssh -o StrictHostKeyChecking=no "$USER" "$CMD"
expect {
timeout {
puts "Timeout happened"
exit 8
}
eof {
puts "EOF received"
exit 4
}
-nocase "password:" {
send "$PASSWORD\n"
expect {
"keys" {
exit 200
}
-nocase "password:" {
exit 1
}
}
}
}
这不起作用:
#!/usr/bin/expect
set USER [lindex $argv 0]
set PASSWORD [lindex $argv 1]
set timeout 10
spawn ssh -o StrictHostKeyChecking=no "$USER" < /var/myscript.sh
# This don't work! ^^^^^^^^^^^^^^^^^^^
expect {
timeout {
puts "Timeout happened"
exit 8
}
eof {
puts "EOF received"
exit 4
}
-nocase "password:" {
send "$PASSWORD\n"
expect {
"keys" {
exit 200
}
-nocase "password:" {
exit 1
}
}
}
}
答案 0 :(得分:2)
<
是shell语法,因此您可以这样做:
spawn bash -c "ssh -o StrictHostKeyChecking=no $USER < /var/myscript.sh"