簡體   English   中英

預期發送腳本錯誤:執行期間未打開Spawn ID exp4

[英]Expect script error send: Spawn id exp4 not open while executing

我正在嘗試運行此腳本,但修改后出現不同的錯誤。 這是代碼和輸出。 請幫忙。

在帖子末尾更新調試信息

    #!/bin/bash
    (( $# != 1 )) && { echo >&2 "Usage: $0 \"[COMMAND]\""; exit 1; }
    servers_addresses=(10.10.10.10 )
    for server_address in ${servers_addresses[@]}; do
    expect <<EOF
    spawn ssh -t root@$server_address "$*"
    expect -timeout 2 "Are you sure you want to continue connecting (yes/no)?" { send "yes\n" }
    expect "s password:" { send "Correct_Password\n" }
    expect "s password:" { send "Wrong_Password_22222\n" }
    expect "s password:" { send "Wrong_Password_33333\n" }
    expect eof
    EOF
    done

輸出是這樣的:

    goldberg188@Test-Server ~$ ./test.sh "sudo cat /etc/hosts"
    spawn ssh -t root@10.10.10.10 sudo cat /etc/hosts
    root@10.10.10.10's password:
    # Do not remove the following line, or various programs
    # that require network functionality will fail.
    10.10.10.10             TEST-004 localhost.localdomain localhost
    ::1             localhost6.localdomain6 localhost6
    Connection to 10.10.10.10 closed.
    expect: spawn id exp4 not open
        while executing
    "expect "s password:" { send "Wrong_Password_33333\n" }"

如果我這樣修改,那么輸出會有些不同

    expect "s password:" { send "Wrong_Password_11111\n" }
    expect "s password:" { send "Correct_Password\n" }
    expect "s password:" { send "Wrong_Password_33333\n" }

    goldberg188@Test-Server ~$ ./test.sh "sudo cat /etc/hosts"
    spawn ssh -t root@10.10.10.10 sudo cat /etc/hosts
    root@10.10.10.10's password:
    # Do not remove the following line, or various programs
    # that require network functionality will fail.
    10.10.10.10             TEST-004 localhost.localdomain localhost
    ::1             localhost6.localdomain6 localhost6
    Connection to 10.10.10.10 closed.
    expect: spawn id exp4 not open
        while executing
    "expect eof"

如果在第三行輸入了正確的密碼,則完全沒有錯誤。 在這一工作正常。

    expect "s password:" { send "Wrong_Password_11111\n" }
    expect "s password:" { send "Wrong_Password_22222\n" }
    expect "s password:" { send "Correct_Password\n" }


    goldberg188@Test-Server ~$ ./test.sh "sudo cat /etc/hosts"
    spawn ssh -t root@10.10.10.10 sudo cat /etc/hosts
    root@10.10.10.10's password:
    # Do not remove the following line, or various programs
    # that require network functionality will fail.
    10.10.10.10             TEST-004 localhost.localdomain localhost
    ::1             localhost6.localdomain6 localhost6
    Connection to 10.10.10.10 closed.

更新:調試信息-修改為

    exp_internal 1
    expect "s password:" { send "Wrong_Password_11111\n" }
    expect "s password:" { send "Correct_Password\n" }
    expect "s password:" { send "Wrong_Password_33333\n" }

輸出:

    goldberg188@Test-Server ~$ ./test.sh "sudo cat /etc/host"
    spawn ssh -t root@10.10.10.10 sudo cat /etc/host
    root@10.10.10.10's password:
    expect: does "root@10.10.10.10's password: " (spawn_id exp4) match glob pattern "s password:"? yes
    expect: set expect_out(0,string) "s password:"
    expect: set expect_out(spawn_id) "exp4"
    expect: set expect_out(buffer) "root@10.10.10.10's password:"
    send: sending "Wrong_Password_11111\n" to { exp4 }

    expect: does " " (spawn_id exp4) match glob pattern "s password:"? no


    expect: does " \r\n" (spawn_id exp4) match glob pattern "s password:"? no
    Permission denied, please try again.
    root@10.10.10.10's password:
    expect: does " \r\nPermission denied, please try again.\r\r\nroot@10.10.10.10's password: " (spawn_id exp4) match glob pattern "s password:"? yes
    expect: set expect_out(0,string) "s password:"
    expect: set expect_out(spawn_id) "exp4"
    expect: set expect_out(buffer) " \r\nPermission denied, please try again.\r\r\nroot@10.10.10.10's password:"
    send: sending "Correct_Password\n" to { exp4 }

    expect: does " " (spawn_id exp4) match glob pattern "s password:"? no


    expect: does " \r\n" (spawn_id exp4) match glob pattern "s password:"? no
    cat: /etc/host: No such file or directory
    Connection to 10.10.10.10 closed.

    expect: does " \r\ncat: /etc/host: No such file or directory\r\r\nConnection to 10.10.10.10 closed.\r\r\n" (spawn_id exp4) match glob pattern "s password:"? no
    expect: read eof
    expect: set expect_out(spawn_id) "exp4"
    expect: set expect_out(buffer) " \r\ncat: /etc/host: No such file or directory\r\r\nConnection to 10.10.10.10 closed.\r\r\n"
    expect: spawn id exp4 not open
        while executing
    "expect eof"

根據您的代碼,在為ssh會話提供密碼后, ssh連接似乎已關閉。

每當使用spawn命令spawn新進程時, expect都會將該期望進程的spawn_id保存到expect_out(spawn_id)

根據您的代碼,expect的spawn_id在遇到時會生成

        spawn ssh -t root@$server_address "$*"  

您所看到的調試如下。

 spawn ssh -t root@10.10.10.10 sudo cat /etc/host
    root@10.10.10.10's password:
    expect: does "root@10.10.10.10's password: " (spawn_id exp4) match glob pattern "s password:"? yes
    expect: set expect_out(0,string) "s password:"
    expect: set expect_out(spawn_id) "exp4"

如您在調試信息中所看到的, expect_out(spawn_id)保留了spawn_id ,對於您的情況,期望值是exp4

如您所見,連接在幾次錯誤的跟蹤之后就關閉了,從而使進程exp4在上下文中不再退出。 由於spawn_id持有對該引用的引用,expect將嘗試從該過程中獲取期望,但失敗了。

您可以參考問題,以了解如何將此spawn_id與標准輸入(正在從控制台讀取輸入)一起使用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM