
私は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 -
のバッファに書き込まれていないことを確認しました。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
。 3番目のパターンは実際には4文字で、3番目の操作は3つのコメントを含むスクリプトを含む文字列です。pattern
action
#"^"
答え2
デフォルトでは、pbrun は端末セッションで実行されます。私はTCLの専門家ではありませんが、シェルスクリプトを介してpbrunを実行するときにpbunの出力を処理するときにpbrunをパイプモードで実行する必要がある場合があります。
"pbrun -p <command> | /usr/local/bin/db_check.sh"
また、pbrunコマンドの転送を処理するときに-n(null入力)を使用したい場合があり、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」メッセージは、stdoutではなくstderrとして返されます。