如何 运行 带有 expect 的 SSH 脚本?
How to run a SSH script with expect?
我可以使用 expect 在远程 SSH 主机上执行命令。一切都很好,只要我将自己限制在一行硬编码 command.However,我想创建一个本地命令脚本以远程执行。
这个有效,但只能使用一行命令:
#!/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
}
}
}
}
<
是 shell 语法,所以你可以这样做:
spawn bash -c "ssh -o StrictHostKeyChecking=no $USER < /var/myscript.sh"
我可以使用 expect 在远程 SSH 主机上执行命令。一切都很好,只要我将自己限制在一行硬编码 command.However,我想创建一个本地命令脚本以远程执行。
这个有效,但只能使用一行命令:
#!/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
}
}
}
}
<
是 shell 语法,所以你可以这样做:
spawn bash -c "ssh -o StrictHostKeyChecking=no $USER < /var/myscript.sh"