6

何が起こっているのかわかりませんが、実行されたリモートコマンドから完全な出力が得られません。おそらく、内部バッファーが実行されていると予想されるためです。

    proc SendCommands { Commands } {
            global prompt log errlog
                    foreach element [split $Commands ";"] {
                                    expect {
                                           -re $prompt
                                            {send -- "$element\r"}
                                           }
                                    set outcome "$expect_out(buffer)"
                             foreach line [split $outcome \n] {
                                       foreach word [regexp -all -inline {\S+} $line] {

                                            if {( [string index [string trimleft $line " "] 0 ] == "%")} {
                                              puts  "$outcome"
                                               exit 1
                                            }
                                      }
                            }
                    puts "$outcome"
                   }

    }

    set timeout 30

    foreach  host [ split $hosts "\;" ] {
            spawn ssh -o "StrictHostKeyChecking no" "$username@$host"
    match_max   10000000

    expect {
      timeout { send_user "\nFailed to get password prompt\n"; exit 1 }
      eof { send_user "\nSSH failure for $host\n"; exit 1 }
           "*?assword:*"
          {
           send -- "$password\r"
          }

    }


    expect {
              timeout { send_user "\nLogin incorrect\n"; exit 1 }
              eof { send_user "\nSSH failure for $host\n"; exit 1 }

         -re  "$prompt"
           { send -- "\r" }
           }
    set timeout 300
    SendCommands "$Commands"
    }

これは私がそれを実行している方法です:

./sendcommand aehj SWEs-elmPCI-B-01.tellus comnet1 "terminal length 0;show int description" "(.*#)$"

削除した場合にのみ完全な出力が得log user 0られますが、上記の関数 sendcommands で puts コマンドを使用すると、その約 90% が得られ、最後の末尾のデータの 10% は表示されません。

私が考えている1つの方法は、expectで正規表現の否定を使用することですが、うまくいかないようです。

                                    expect {
                                           -re ! $prompt
                                            {puts $expect_outcome(buffer)}
                                           }

編集:約5回または7回実行すると、すべての出力が1回取得されます

4

1 に答える 1

2

少し検索した後、私はこれを思いつき、うまくいくようですが、例外やより良い答えを教えてください:

次に、match_max = 1000 に設定します

    expect {
           -re $prompt
          {send -- "$element\r"}

    full_buffer {
        append outcome $expect_out(buffer)
        exp_continue
    }

                                    }
append outcome $expect_out(buffer)
puts $outcome

しかし、まだ match_max = 10000 または 100 に設定すると、再び失敗します

于 2013-05-02T10:39:36.350 に答える