我注意到我的 TCL/Expect 脚本没有写入输出缓冲区。
`#!/usr/bin/expect -f
exp_internal 1; #Debug Expect
set username [lindex $argv 0]; #Takes Outside Argument
set password [lindex $argv 1]; #Takes Outside Argument
set hostFile [open hosts.txt r]; #Reads from a local file
while { [gets $hostFile hostname] >= 0 } {
spawn ssh -q -o StrictHostKeyChecking=no $username@$hostname
expect "password: "
send "$password\r"
expect "$ "
send "pbrun /bin/su -"
expect {
"Operations Team.*" {
puts "NewLine Was Caught After pbrun /bin/su - command"
#break #Exit While loop, which will exit the server since there is only one While Loop aprund the whole SSH Session
}
"Rejected.*" {
puts "NewLine & Return Carraige Was Caught After pbrun /bin/su - command"
#break #Exit While loop, which will exit the server since there is only one While Loop aprund the whole SSH Session
}
#"^" { ;#This command will work because in the expect only picks up a "^" character from the [send "pbrun /bin/su -"] command. This is an issues as the pbrun command has more ouput that just a "^", but expect only picks up the "^" character. You can use any other command like "ls", and expect does not write the ls command ouput to the buffer, only a newline character will be written to the buffer. This is an issue as I am unable to grab anything from expect.
# puts "Newline Character Met"
#}
}
send "hostname\r"
expect {
-re {".{8}[0-9]{5}"} {; #Never Works
puts "BUFFER:$expect_out(buffer):REFFUB"
}
"^" {; #Again, this will be met
puts "Newline Character Met"
}
}
}`
任何帮助深表感谢。我在调试模式下运行它,确实看到除了字符之外,没有任何输出被写入pbrun /bin/su -
or命令的缓冲区。hostname
^
答案1
您的实际问题:该模式{".{8}[0-9]{5}"}
将匹配包含双引号、后跟 8 个字符、后跟 5 个数字、后跟双引号的字符串 - 这些双引号是否应该出现在您的模式中?有时人们会对 Tcl 的引用感到困惑,所以我的问题并不意味着带有任何判断。
另外,当您在 Expect 块中有 2 个模式时,任何一个都可以第一个匹配:您是否期望看到 8 个字符和 5 个数字前第一个换行符?
等等,^
将在第一行的开头匹配。如果您想显式匹配换行符,请使用\n
.
不幸的是,Tcl 的注释处理可能会令人困惑。评论字符#
仅有的开始评论如果看到命令可以去的地方。
您在期望块中放入的注释不要注释掉这些行。
expect {
"Operations Team.*" {
puts "NewLine Was Caught After pbrun /bin/su - command"
#break #Exit While loop, which will exit the server since there is only one While Loop aprund the whole SSH Session
}
"Rejected.*" {
puts "NewLine & Return Carraige Was Caught After pbrun /bin/su - command"
#break #Exit While loop, which will exit the server since there is only one While Loop aprund the whole SSH Session
}
#"^" { ;#This command will work because in the expect only picks up a "^" character from the [send "pbrun /bin/su -"] command. This is an issues as the pbrun command has more ouput that just a "^", but expect only picks up the "^" character. You can use any other command like "ls", and expect does not write the ls command ouput to the buffer, only a newline character will be written to the buffer. This is an issue as I am unable to grab anything from expect.
# puts "Newline Character Met"
#}
}
您实质上是向命令传递一个包含和对的expect
列表。第三个模式实际上是 4 个字符,第三个操作是一个包含脚本的字符串,该脚本包含 3 个注释。pattern
action
#"^"
答案2
默认情况下,pbrun 在终端会话中运行。我不是TCL专家,但是通过shell脚本运行pbrun时,pbrun在处理pbun的输出时有时需要以管道模式运行。
"pbrun -p <command> | /usr/local/bin/db_check.sh"
此外,在处理发送 pbrun 命令时,您可能还想使用 -n (空输入),并且没有通过 stdin 的输入,但由于调用 pbrun 的脚本不干净,所以 pbrun 会以某种方式获取一些缓冲数据。
-n, --null_input
Redirects the standard input of pbrun to /dev/null. You some‐
times need this option to avoid interactions between pbrun and
the shell that invokes it. For example, if you are running
pbrun and start a pbrun in the background without redirecting
its input away from the terminal, it will block even if no reads
are posted by the remote command. This option corrects this
situation.
-p, --pipe_mode
Pipe mode. Forces the secured task to behave as if it is run in
a pipeline rather than a terminal session. This option also
suppresses utmp entries, so that the session will not be
recorded as a login on the runhost.
最后一项,“Rejected”消息返回到 stderr,而不是 stdout。