実行時に「コマンド名が正しくありません」が発生し、予想されるエラー

実行時に「コマンド名が正しくありません」が発生し、予想されるエラー

エラーメッセージが表示されます。

 invalid command name "
    while executing
""\r""
    invoked from within
"expect {
    -ex "--More--" {send -- " "; exp_continue}
     "*>" {send "exit \r";"\r"}
}"
    (file "./scriptprueba4.sh" line 12)

expectうまく動作していたスクリプトから。情報を取得するためにスクリプトを修正しましたが、show CDP neighborエラーが発生しました。

コードは次のとおりです。

#!/usr/bin/expect -f
#!/bin/sh
set DATE [exec date +%F]
spawn telnet 111.111.1.111
expect "Username:"
send "user\r"
expect "Password: "
send "this.is.the.pass\r"
expect "*>"
send "show cdp neighbors \r"
log_file -noappend CDP.dat
expect {                               #this is the part i just modified and caused the error.
    "More--" {send " "; exp_continue}
    ">" {send "exit \r"; "\r"}
}
expect "$ "                            #I want this part of the code to be executed after "$" but the script stops before doing it 
send "tail -n+6 CDP.dat > CDP2.dat\r"
expect "$ "
send "./primeros3.sh \r"
expect "$ "
send -- "tr -d '\\r' < CDPyPuerto.dat | awk '{ printf \"%s %s\", \$0, (length(\$1) > 16) ? OFS : ORS}' > TablaCDP.dat \r"
expect "$ "

機能しないコード部分は、マルチページ端末出力を処理することです。スクリプトは、次の場合まで正しく機能します。

expect {                               
    "More--" {send " "; exp_continue}
    ">" {send "exit \r"; "\r"}
}

助けが必要ですか?

更新:コードを次のように変更しました。

#!/usr/bin/expect -f
#!/bin/sh
set DATE [exec date +%F]
spawn telnet 111.111.1.111
expect "Username:"
send "user\r"
expect "Password: "
send "this.is.the.pass\r"
expect "*>"
send "show cdp neighbors \r"
log_file -noappend CDP.dat
expect -ex "--More--" {send -- " "; exp_continue}
expect "*>"
send "exit \r"
expect "$ "
send "tail -n+6 CDP.dat > CDP2.dat\r"
expect "$ "
send "./primeros3.sh \r"
expect "$ "
send -- "tr -d '\\r' < CDPyPuerto.dat | awk '{ printf \"%s %s\", \$0, (length(\$1) > 16) ? OFS : ORS}' > TablaCDP.dat \r"
expect "$ "

これでエラーが表示されます。

send: spawn id exp4 not open
    while executing
"send "tail -n+6 CDP.dat > CDP2.dat\r""
    (file "./scriptprueba4.sh" line 16)

ベストアンサー1

おすすめ記事