Bash 期望匹配 bash 和 ksh 提示
Bash expect matching bash and ksh prompts
我有多个带有 bash 和 ksh 提示的系统:
bash 提示如下:[user1@wanserver bin]$
ksh 提示看起来像:在某些系统上是 $,在某些系统上是目录名>
使用 expect 我如何匹配这些 ksh 提示。我能够匹配 bash 提示,但不能匹配 ksh 提示。我在下面尝试了 ksh,但它不起作用。如何让它发挥作用?
#!/usr/bin/expect
set username [lindex $argv 0]
set password [lindex $argv 1]
set rt [lindex $argv 2]
spawn ssh -q -o StrictHostKeyChecking=no $username@$rt
expect {
timeout {
puts "\n\nConnection timed out"
exit 1
}
"*assword:" {
send "$password\r"
}
}
expect {
"$ " {
send "ls -l\r"
}
}
而不是:
expect {
"$ " {
send "ls -l\r"
}
使用:
expect {
{$ } {
send "ls -l\r"
}
有关详细信息,请参阅 http://tcl-lang.org/man/tcl/TclCmd/Tcl.htm
中的规则 4、6 和 8
假设在所有提示后有一个space,你可以使用正则表达式:
set prompt {[$>] $}
#...
expect -re $prompt
send "ls -l\r"
第一个美元符号是字面美元符号,因为它在括号表达式中。第二个是正则表达式 "end of line" 锚点。
我有多个带有 bash 和 ksh 提示的系统: bash 提示如下:[user1@wanserver bin]$ ksh 提示看起来像:在某些系统上是 $,在某些系统上是目录名>
使用 expect 我如何匹配这些 ksh 提示。我能够匹配 bash 提示,但不能匹配 ksh 提示。我在下面尝试了 ksh,但它不起作用。如何让它发挥作用?
#!/usr/bin/expect
set username [lindex $argv 0]
set password [lindex $argv 1]
set rt [lindex $argv 2]
spawn ssh -q -o StrictHostKeyChecking=no $username@$rt
expect {
timeout {
puts "\n\nConnection timed out"
exit 1
}
"*assword:" {
send "$password\r"
}
}
expect {
"$ " {
send "ls -l\r"
}
}
而不是:
expect {
"$ " {
send "ls -l\r"
}
使用:
expect {
{$ } {
send "ls -l\r"
}
有关详细信息,请参阅 http://tcl-lang.org/man/tcl/TclCmd/Tcl.htm
中的规则 4、6 和 8假设在所有提示后有一个space,你可以使用正则表达式:
set prompt {[$>] $}
#...
expect -re $prompt
send "ls -l\r"
第一个美元符号是字面美元符号,因为它在括号表达式中。第二个是正则表达式 "end of line" 锚点。