TCL は出力バッファに書き込むことは期待されません。

TCL は出力バッファに書き込むことは期待されません。

私は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つのコメントを含むスクリプトを含む文字列です。patternaction#"^"

おすすめ記事