Linux expect 脚本有时以“外部主机关闭连接”结尾,这是 expect 脚本还是设备问题?

Linux expect script sometimes ending with `Connection closed by foreign host`, is this expect script or device issue?

我是运行ning CentOS 7.6

$ cat /etc/system-release
CentOS Linux release 7.6.1810 (Core)

这是 repos 中 expect 的最新版本。

$ expect -version
expect version 5.45

根据你的建议,我已经调整了我的代码以使其更容易修复,身份验证部分应该不是问题所以我暂时删除了它,下面是我期望的代码,测试没有的设备身份验证。

$ cat runCommands.exp
spawn telnet 10.220.64.67
set prompt "#"
expect ">"
send "enable\r"
expect $prompt
send "terminal length 200\r"
expect $prompt
send "terminal more off\r"
expect $prompt
send "show hw-inventory details\r"
expect $prompt
send "exit\r"
expect eof

这就是我 运行我的 expect 脚本的方式,它正确启动但最后它只是停止。

$ expect -f ./runCommands.exp
spawn telnet 10.220.64.67
Trying 10.220.64.67...
Connected to 10.220.64.67.
Escape character is '^]'.

******************************************************************
*                                                                *
*                  Tellabs 8609 Network Element                  *
*                                                                *
*     Copyright (c) 2004-2015 Tellabs. All rights reserved.      *
*                                                                *
******************************************************************
Enter configuration commands, one per line. End with ^Z
HENKEL_CAPITAL_2-EQU>enable
HENKEL_CAPITAL_2-EQU#terminal length 200
HENKEL_CAPITAL_2-EQU#terminal more off
HENKEL_CAPITAL_2-EQU#show hw-inventory details
This is node 1643 of type 8609 and subrack address 0.
    unit in slot 1 is part of inventory and UP AND RUNNING
        unit 1 of width 1 has serial number 40601444713, CLEI code IPMVU00DRB
        top level serial number 40601445792
        manufacturing date 2016-3-30, 4:42:19.0, +0:00
        part# 82.86S-8609-R6 rev C
        HW type sbb2284-8609(2284) v3.0.1 SW type 0 v2.0.331
            Pmax=70.0W, Ptyp=60.0W
        infomodel FP2.0 (2.2.0); internal SW v.2.0.331
            Supported infomodels:
                FP2.0 (2.2.0)
        CPLD active revision: 0x0130
        WPIF expected and active revision: 0x001a
        LMIF expected and active revision: 0x0023
        TOCK expected and active revision: 0x0416
        module IF0 (LEFT) of type 4*100/1000BASE-X(9094)
            SFP connector 0 data:
                [0] type of serial transceiver: 3 (SFP transceiver)
                [1] extended identifier of type of serial transceiver: 4
                [2] connector type: 7(LC)
                [3] infiniband compliance code: 0
                [4] sonet compliance code4: 0
                [5] sonet compliance code5: 0
                [6] gigabit ethernet compliance code: 2, 1000BASE-LX
                [11] serial encoding type: 1 8B10B
                [14] link length for 9/125mm fiber (km): 52
                [15] link length for 9/125mm fiber (100m): 255
                [16] link length for 50/125mm fiber (10m): 0
                [17] link length for 62.5/125mm fiber (10m): 0
                [18] link length for copper (m): 0
                [20] vendor name: FINISAR CORP.
                [37] vendor organizationally unique: 36965
                [40] vendor part number: FTLF1419P1BCL-TL
                [56] vendor revision number: A
                [60] Laser wavelength: 1310 nm
                [62] reserved field: 0
                [68] vendor serial number: PU117T5
                [84] vendor's date (yy-mm-dd): 15-07-03
                [90] vendor's date (vendor specific part):
                [224-241] Tellabs part number: 81.86T-S001-13S-R6
                [242-243] Tellabs revision:  A
                [244-253] Tellabs CLEI code:  IPU3AVSKAA
                [255] Tellabs checksum: 0x3C
                [d102] Tx power: 2.6 dBm (1.8 mW)
                [d104] Rx power: -7.8 dBm (0.17 mW)
            SFP connector 1 is not present
            SFP connector 2 is not present
            SFP connector 3 is not present
        module IF1 (MIDDLE LEFT) of type 4*100/1000BASE-X(9094)
            SFP connector 0 data:
                [0] type of serial transceiver: 3 (SFP transceiver)
                [1] extended identifier of type of serial transceiver: 4
                [2] connector type: 7(LC)
                [3] infiniband compliance code: 0
                [4] sonet compliance code4: 0
                [5] sonet compliance code5: 0
                [6] gigabit ethernet compliance code: 2, 1000BASE-LX
                [11] serial encoding type: 1 8B10B
                [14] link length for 9/125mm fiber (km): 40
                [15] link length for 9/125mm fiber (100m): 0
                [16] link length for 50/125mm fiber (10m): 0
                [17] link length for 62.5/125mm fiber (10m): 0
                [18] link length for copper (m): 0
                [20] vendor name: HG GENUINE
                [37] vendor organizationally Connection closed by foreign host.

正如您在上面看到的,我收到的错误是 外部主机关闭的连接,我无法获得 show [=] 的完整输出57=] details 命令,不确定输出的大小是否会影响它。

作为我所知道的用于调试此 expect 脚本的唯一资源是添加 -d 标志我有 运行 与该选项完全相同的脚本以防我们可以了解这里发生的事情。

$ expect -df ./runCommands.exp
expect version 5.45
argv[0] = expect  argv[1] = -df  argv[2] = ./runCommands.exp
set argc 0
set argv0 "./runCommands.exp"
set argv ""
executing commands from command file ./runCommands.exp
spawn telnet 10.220.64.67
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {15767}

expect: does "" (spawn_id exp6) match glob pattern ">"? no
Trying 10.220.64.67...

expect: does "Trying 10.220.64.67...\r\r\n" (spawn_id exp6) match glob pattern ">"? no
Connected to 10.220.64.67.
Escape character is '^]'.

expect: does "Trying 10.220.64.67...\r\r\nConnected to 10.220.64.67.\r\r\nEscape character is '^]'.\r\r\n" (spawn_id exp6) match glob pattern ">"? no

******************************************************************
*                                                                *
*                  Tellabs 8609 Network Element                  *
*                                                                *
*     Copyright (c) 2004-2015 Tellabs. All rights reserved.      *
*                                                                *
******************************************************************
Enter configuration commands, one per line. End with ^Z
HENKEL_CAPITAL_2-EQU>
expect: does "Trying 10.220.64.67...\r\r\nConnected to 10.220.64.67.\r\r\nEscape character is '^]'.\r\r\n\r\r\n******************************************************************\r\r\n*                                                                *\r\r\n*                  Tellabs 8609 Network Element                  *\r\r\n*                                                                *\r\r\n*     Copyright (c) 2004-2015 Tellabs. All rights reserved.      *\r\r\n*                                                                *\r\r\n******************************************************************\r\r\nEnter configuration commands, one per line. End with ^Z\r\nHENKEL_CAPITAL_2-EQU>" (spawn_id exp6) match glob pattern ">"? yes
expect: set expect_out(0,string) ">"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "Trying 10.220.64.67...\r\r\nConnected to 10.220.64.67.\r\r\nEscape character is '^]'.\r\r\n\r\r\n******************************************************************\r\r\n*                                                                *\r\r\n*                  Tellabs 8609 Network Element                  *\r\r\n*                                                                *\r\r\n*     Copyright (c) 2004-2015 Tellabs. All rights reserved.      *\r\r\n*                                                                *\r\r\n******************************************************************\r\r\nEnter configuration commands, one per line. End with ^Z\r\nHENKEL_CAPITAL_2-EQU>"
send: sending "enable\r" to { exp6 }

expect: does "" (spawn_id exp6) match glob pattern "#"? no
HENKEL_CAPITAL_2-EQU>
expect: does "\r                     \u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008HENKEL_CAPITAL_2-EQU>" (spawn_id exp6) match glob pattern "#"? no
enable

expect: does "\r                     \u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008HENKEL_CAPITAL_2-EQU>enable\r\n" (spawn_id exp6) match glob pattern "#"? no
HENKEL_CAPITAL_2-EQU#
expect: does "\r                     \u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008HENKEL_CAPITAL_2-EQU>enable\r\nHENKEL_CAPITAL_2-EQU#" (spawn_id exp6) match glob pattern "#"? yes
expect: set expect_out(0,string) "#"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "\r                     \u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008HENKEL_CAPITAL_2-EQU>enable\r\nHENKEL_CAPITAL_2-EQU#"
send: sending "terminal length 200\r" to { exp6 }

expect: does "" (spawn_id exp6) match glob pattern "#"? no
terminal length 200

expect: does "terminal length 200\r\n" (spawn_id exp6) match glob pattern "#"? no
HENKEL_CAPITAL_2-EQU#
expect: does "terminal length 200\r\nHENKEL_CAPITAL_2-EQU#" (spawn_id exp6) match glob pattern "#"? yes
expect: set expect_out(0,string) "#"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "terminal length 200\r\nHENKEL_CAPITAL_2-EQU#"
send: sending "terminal more off\r" to { exp6 }

expect: does "" (spawn_id exp6) match glob pattern "#"? no
terminal more off

expect: does "terminal more off\r\n" (spawn_id exp6) match glob pattern "#"? no
HENKEL_CAPITAL_2-EQU#
expect: does "terminal more off\r\nHENKEL_CAPITAL_2-EQU#" (spawn_id exp6) match glob pattern "#"? yes
expect: set expect_out(0,string) "#"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "terminal more off\r\nHENKEL_CAPITAL_2-EQU#"
send: sending "show hw-inventory details\r" to { exp6 }

expect: does "" (spawn_id exp6) match glob pattern "#"? no
show hw-inventory details

expect: does "show hw-inventory details\r\n" (spawn_id exp6) match glob pattern "#"? no
This is node 1643 of type 8609 and subrack address 0.
    unit in slot 1 is pa
expect: does "show hw-inventory details\r\nThis is node 1643 of type 8609 and subrack address 0.\r\n    unit in slot 1 is pa" (spawn_id exp6) match glob pattern "#"? no
r
expect: does "show hw-inventory details\r\nThis is node 1643 of type 8609 and subrack address 0.\r\n    unit in slot 1 is par" (spawn_id exp6) match glob pattern "#"? no
t of inventory and UP AND RUNNING
        unit 1 of width 1 has serial number 4
expect: does "show hw-inventory details\r\nThis is node 1643 of type 8609 and subrack address 0.\r\n    unit in slot 1 is part of inventory and UP AND RUNNING\r\n        unit 1 of width 1 has serial number 4" (spawn_id exp6) match glob pattern "#"? no
0601444713, CLEI code IPMVU00DRB
        top level serial number 40601445792
        manufacturing date 2016-3-30, 4:42:19.0, +0:00
        part# 82.86S-8609
expect: does "show hw-inventory details\r\nThis is node 1643 of type 8609 and subrack address 0.\r\n    unit in slot 1 is part of inventory and UP AND RUNNING\r\n        unit 1 of width 1 has serial number 40601444713, CLEI code IPMVU00DRB\r\n        top level serial number 40601445792\r\n        manufacturing date 2016-3-30, 4:42:19.0, +0:00\r\n        part# 82.86S-8609" (spawn_id exp6) match glob pattern "#"? yes
expect: set expect_out(0,string) "#"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "show hw-inventory details\r\nThis is node 1643 of type 8609 and subrack address 0.\r\n    unit in slot 1 is part of inventory and UP AND RUNNING\r\n        unit 1 of width 1 has serial number 40601444713, CLEI code IPMVU00DRB\r\n        top level serial number 40601445792\r\n        manufacturing date 2016-3-30, 4:42:19.0, +0:00\r\n        part#"
send: sending "exit\r" to { exp6 }
-R6 rev C
        HW type sbb2284-8609(2284) v3.0.1 SW type 0 v2.0.331
            Pmax=70.0W, Ptyp=60.0W
        infomodel FP2.0 (2.2.0); internal SW v.2.0.331
            Supported infomodels:
                FP2.0 (2.2.0)
        CPLD active revision: 0x0130
        WPIF expected and active revision: 0x001a
        LMIF expected and active revision: 0x0023
        TOCK expected and active revision: 0x0416
        module IF0 (LEFT) of type 4*100/1000BASE-X(9094)
            SFP connector 0 data:
                [0] type of serial transceiver: 3 (SFP transceiver)
                [1] extended identifier of type of serial transceiver: 4
                [2] connector type: 7(LC)
                [3] infiniband compliance code: 0
                [4] sonet compliance code4: 0
                [5] sonet compliance code5: 0
                [6] gigabit ethernet compliance code: 2, 1000BASE-LX
                [11] serial encoding type: 1 8B10B
                [14] link length for 9/125mm fiber (km): 52
                [15] link length for 9/125mm fiber (100m): 255
                [16] link length for 50/125mm fiber (10m): 0
                [17] link length for 62.5/125mm fiber (10m): 0
                [18] link length for copper (m): 0
                [20] vendor name: FINISAR CORP.
                [37] vendor organizationally unique: 36965
                [40] vendor part number: FTLF1419P1BCL-TL
                [56] vendor revision number: A
                [60] Laser wavelength: 1310 nm
                [62] reserved field: 0
                [68] vendor serial number: PU117T5
                [84] vendor's date (yy-mm-dd): 15-07-03
                [90] vendor's date (vendor specific part):
                [224-241] Tellabs part number: 81.86T-S001-13S-R6
                [242-243] Tellabs revision:  A
                [244-253] Tellabs CLEI code:  IPU3AVSKAA
                [255] Tellabs checksum: 0x3C
                [d102] Tx power: 2.6 dBm (1.8 mW)
                [d104] Rx power: -7.7 dBm (0.17 mW)
            SFP connector 1 is not present
            SFP connector 2 is not present
            SFP connector 3 is not present
        module IF1 (MIDDLE LEFT) of type 4*100/1000BASE-X(9094)
            SFP connector 0 data:
                [0] type of serial transceiver: 3 (SFP transceiver)
                [1] extended identifier of type of serial transceiver: 4
                [2] connector type: 7(LC)
                [3] infiniband compliance code: 0
                [4] sonet compliance code4: 0
                [5] sonet compliance code5: 0
                [6] gigabit ethernet compliance code: 2, 1000BASE-LX
                [11] serial encoding type: 1 8B10B
                [14] link length for 9/125mm fiber (km): 40
                [15] link length for 9/125mm fiber (100m): 0
                [16] link length for 50/125mm fiber (10m): 0
                [17] link length for 62.5/125mm fiber (10m): 0
                [18] link length for copper (m): 0
                [20] vendor name: HG GENUINE
                [37] vendor organizationally unique: 0
                [40] vendor part number: MXPD-243MD
                [56] vendor revision number:
                [60] Laser wavelength: 1310 nm
                [62] reserved field: 0
                [68] vendor serial number: ME12112000011
                [84] vendor's date (yy-mm-dd): 12-03-17
                [90] vendor's date (vendor specific part):
                [224-255] vendor specific ID field : 0x00000000
                    0x00000000 0x00000000 0x00000000 0x00000000
                    0x00000000 0x00000000 0x00000000
                [d102] Tx power: -3.0 dBm (0.50 mW)
                [d104] Rx power: -13 dBm (50 uW)
            SFP connector 1 is not present
            SFP connector 2 is Connection closed by foreign host.
expect: read eof
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) " 82.86S-8609-R6 rev C\r\n        HW type sbb2284-8609(2284) v3.0.1 SW type 0 v2.0.331\r\n            Pmax=70.0W, Ptyp=60.0W\r\n        infomodel FP2.0 (2.2.0); internal SW v.2.0.331\r\n            Supported infomodels:\r\n                FP2.0 (2.2.0)\r\n        CPLD active revision: 0x0130\r\n        WPIF expected and active revision: 0x001a\r\n        LMIF expected and active revision: 0x0023\r\n        TOCK expected and active revision: 0x0416\r\n        module IF0 (LEFT) of type 4*100/1000BASE-X(9094)\r\n            SFP connector 0 data:\r\n                [0] type of serial transceiver: 3 (SFP transceiver)\r\n                [1] extended identifier of type of serial transceiver: 4\r\n                [2] connector type: 7(LC)\r\n                [3] infiniband compliance code: 0\r\n                [4] sonet compliance code4: 0\r\n                [5] sonet compliance code5: 0\r\n                [6] gigabit ethernet compliance code: 2, 1000BASE-LX\r\n                [11] serial encoding type: 1 8B10B\r\n                [14] link length for 9/125mm fiber (km): 52\r\n                [15] link length for 9/125mm fiber (100m): 255\r\n                [16] link length for 50/125mm fiber (10m): 0\r\n                [17] link length for 62.5/125mm fiber (10m): 0\r\n                [18] link length for copper (m): 0\r\n                [20] vendor name: FINISAR CORP.   \r\n                [37] vendor organizationally unique: 36965\r\n                [40] vendor part number: FTLF1419P1BCL-TL\r\n                [56] vendor revision number: A   \r\n                [60] Laser wavelength: 1310 nm\r\n                [62] reserved field: 0\r\n                [68] vendor serial number: PU117T5         \r\n                [84] vendor's date (yy-mm-dd): 15-07-03\r\n                [90] vendor's date (vendor specific part):   \r\n                [224-241] Tellabs part number: 81.86T-S001-13S-R6\r\n                [242-243] Tellabs revision:  A \r\n                [244-253] Tellabs CLEI code:  IPU3AVSKAA\r\n                [255] Tellabs checksum: 0x3C\r\n                [d102] Tx power: 2.6 dBm (1.8 mW)\r\n                [d104] Rx power: -7.7 dBm (0.17 mW)\r\n            SFP connector 1 is not present\r\n            SFP connector 2 is not present\r\n            SFP connector 3 is not present\r\n        module IF1 (MIDDLE LEFT) of type 4*100/1000BASE-X(9094)\r\n            SFP connector 0 data:\r\n                [0] type of serial transceiver: 3 (SFP transceiver)\r\n                [1] extended identifier of type of serial transceiver: 4\r\n                [2] connector type: 7(LC)\r\n                [3] infiniband compliance code: 0\r\n                [4] sonet compliance code4: 0\r\n                [5] sonet compliance code5: 0\r\n                [6] gigabit ethernet compliance code: 2, 1000BASE-LX\r\n                [11] serial encoding type: 1 8B10B\r\n                [14] link length for 9/125mm fiber (km): 40\r\n                [15] link length for 9/125mm fiber (100m): 0\r\n                [16] link length for 50/125mm fiber (10m): 0\r\n                [17] link length for 62.5/125mm fiber (10m): 0\r\n                [18] link length for copper (m): 0\r\n                [20] vendor name: HG GENUINE      \r\n                [37] vendor organizationally unique: 0\r\n                [40] vendor part number: MXPD-243MD      \r\n                [56] vendor revision number: \r\n                [60] Laser wavelength: 1310 nm\r\n                [62] reserved field: 0\r\n                [68] vendor serial number: ME12112000011   \r\n                [84] vendor's date (yy-mm-dd): 12-03-17\r\n                [90] vendor's date (vendor specific part):   \r\n                [224-255] vendor specific ID field : 0x00000000 \r\n                    0x00000000 0x00000000 0x00000000 0x00000000 \r\n                    0x00000000 0x00000000 0x00000000 \r\n                [d102] Tx power: -3.0 dBm (0.50 mW)\r\n                [d104] Rx power: -13 dBm (50 uW)\r\n            SFP connector 1 is not present\r\n            SFP connector 2 is Connection closed by foreign host.\r\r\n"

您认为这可能是 expect 脚本本身的问题,还是与 telnet 设备有关的其他问题?

如果你愿意,我们可以screen session,我们可以一起解决这个问题,此时我对这个奇怪的问题感到绝望,不知道如何保持前进。

这是完整的预期输出:

$ telnet 10.220.64.67
Trying 10.220.64.67...
Connected to 10.220.64.67.
Escape character is '^]'.

******************************************************************
*                                                                *
*                  Tellabs 8609 Network Element                  *
*                                                                *
*     Copyright (c) 2004-2015 Tellabs. All rights reserved.      *
*                                                                *
******************************************************************
Enter configuration commands, one per line. End with ^Z
HENKEL_CAPITAL_2-EQU>enable
HENKEL_CAPITAL_2-EQU#show hw-inventory details
This is node 1643 of type 8609 and subrack address 0.
    unit in slot 1 is part of inventory and UP AND RUNNING
        unit 1 of width 1 has serial number 40601444713, CLEI code IPMVU00DRB
        top level serial number 40601445792
        manufacturing date 2016-3-30, 4:42:19.0, +0:00
        part# 82.86S-8609-R6 rev C
        HW type sbb2284-8609(2284) v3.0.1 SW type 0 v2.0.331
            Pmax=70.0W, Ptyp=60.0W
        infomodel FP2.0 (2.2.0); internal SW v.2.0.331
            Supported infomodels:
                FP2.0 (2.2.0)
        CPLD active revision: 0x0130
        WPIF expected and active revision: 0x001a
        LMIF expected and active revision: 0x0023
        TOCK expected and active revision: 0x0416
        module IF0 (LEFT) of type 4*100/1000BASE-X(9094)
            SFP connector 0 data:
                [0] type of serial transceiver: 3 (SFP transceiver)
                [1] extended identifier of type of serial transceiver: 4
                [2] connector type: 7(LC)
                [3] infiniband compliance code: 0
                [4] sonet compliance code4: 0
                [5] sonet compliance code5: 0
                [6] gigabit ethernet compliance code: 2, 1000BASE-LX
                [11] serial encoding type: 1 8B10B
                [14] link length for 9/125mm fiber (km): 52
                [15] link length for 9/125mm fiber (100m): 255
                [16] link length for 50/125mm fiber (10m): 0
                [17] link length for 62.5/125mm fiber (10m): 0
                [18] link length for copper (m): 0
                [20] vendor name: FINISAR CORP.
                [37] vendor organizationally unique: 36965
                [40] vendor part number: FTLF1419P1BCL-TL
                [56] vendor revision number: A
                [60] Laser wavelength: 1310 nm
                [62] reserved field: 0
                [68] vendor serial number: PU117T5
                [84] vendor's date (yy-mm-dd): 15-07-03
                [90] vendor's date (vendor specific part):
                [224-241] Tellabs part number: 81.86T-S001-13S-R6
                [242-243] Tellabs revision:  A
                [244-253] Tellabs CLEI code:  IPU3AVSKAA
                [255] Tellabs checksum: 0x3C
                [d102] Tx power: 2.6 dBm (1.8 mW)
                [d104] Rx power: -7.7 dBm (0.17 mW)
            SFP connector 1 is not present
            SFP connector 2 is not present
            SFP connector 3 is not present
        module IF1 (MIDDLE LEFT) of type 4*100/1000BASE-X(9094)
            SFP connector 0 data:
                [0] type of serial transceiver: 3 (SFP transceiver)
                [1] extended identifier of type of serial transceiver: 4
                [2] connector type: 7(LC)
                [3] infiniband compliance code: 0
                [4] sonet compliance code4: 0
                [5] sonet compliance code5: 0
                [6] gigabit ethernet compliance code: 2, 1000BASE-LX
                [11] serial encoding type: 1 8B10B
                [14] link length for 9/125mm fiber (km): 40
                [15] link length for 9/125mm fiber (100m): 0
                [16] link length for 50/125mm fiber (10m): 0
                [17] link length for 62.5/125mm fiber (10m): 0
                [18] link length for copper (m): 0
                [20] vendor name: HG GENUINE
                [37] vendor organizationally unique: 0
                [40] vendor part number: MXPD-243MD
                [56] vendor revision number:
                [60] Laser wavelength: 1310 nm
                [62] reserved field: 0
                [68] vendor serial number: ME12112000011
                [84] vendor's date (yy-mm-dd): 12-03-17
                [90] vendor's date (vendor specific part):
                [224-255] vendor specific ID field : 0x00000000
                    0x00000000 0x00000000 0x00000000 0x00000000
                    0x00000000 0x00000000 0x00000000
                [d102] Tx power: -3.0 dBm (0.50 mW)
                [d104] Rx power: -13 dBm (50 uW)
            SFP connector 1 is not present
            SFP connector 2 is not present
            SFP connector 3 is not present
        module IF2 (MIDDLE) of type 4*10/100/1000BASE-T(9093)
        module IF3 (RIGHT MIDDLE) of type 8*10/100BASE-TX-LM(2243) v2.0.1 (hot insert)
            serial number 40601373354, CLEI code IPU3A7TKAA
            top level serial number 40601376329
            manufacturing date 2015-10-20, 8:51:59.0, +0:00
            part# 81.86LFETX82243-R6 rev D
            Pmax=4.60W, Ptyp=4.00W
        module IF4 (RIGHT) of type 8*10/100BASE-TX-LM(2243) v2.0.1 (hot insert)
            serial number 40601405399, CLEI code IPU3A7TKAA
            top level serial number 40601405948
            manufacturing date 2015-12-31, 0:06:43.0, +0:00
            part# 81.86LFETX82243-R6 rev D
            Pmax=4.60W, Ptyp=4.00W
        module COOLING FAN 1 of type fmb2285-coolingFan(2285)
        module PRIMARY POWER of type -48Vdc(2264)(2264) v2.0.0
            serial number 40601425507, CLEI code IPUPAKMLAA
            top level serial number 40601430234
            manufacturing date 2016-2-26, 17:02:48.9, +8:00
            part# 81.86P-0911DC48-R6 rev C
            Pmax=0.00W, Ptyp=0.00W
        module OVEN CONTROLLED OSCILLATOR of type vid9096-ocxo(9096) v1.0.0
        interface module 0 has
            expected type 4*100/1000BASE-X
            active   type 4*100/1000BASE-X
            existing type 4*100/1000BASE-X
            mode is not relevant
        interface module 1 has
            expected type 4*100/1000BASE-X
            active   type 4*100/1000BASE-X
            existing type 4*100/1000BASE-X
            mode is not relevant
        interface module 2 has
            expected type 4*10/100/1000BASE-T
            active   type 4*10/100/1000BASE-T
            existing type 4*10/100/1000BASE-T
            mode is not relevant
        interface module 3 has
            expected type 8*10/100BASE-TX-LM
            active   type 8*10/100BASE-TX-LM
            existing type 8*10/100BASE-TX-LM
            mode is not relevant
            Pmax=4.60W, Ptyp=4.00W
        interface module 4 has
            expected type 8*10/100BASE-TX-LM
            active   type 8*10/100BASE-TX-LM
            existing type 8*10/100BASE-TX-LM
            mode is not relevant
            Pmax=4.60W, Ptyp=4.00W
        power module primary has
            expected type -48Vdc(2264)
            active   type -48Vdc(2264)
            existing type -48Vdc(2264)
            mode is not relevant
            Pmax=0.00W, Ptyp=0.00W
        oven controlled oscillator has
            expected type vid9096-ocxo
            active   type vid9096-ocxo
            existing type vid9096-ocxo
            mode is not relevant

HENKEL_CAPITAL_2-EQU#exit
Connection closed by foreign host.

在之前的输出中,我手动输入了命令并且它工作正常,唯一的区别是我没有使用 terminal more offterminal length 200 个命令。

$ telnet 10.220.64.67
Enter configuration commands, one per line. End with ^Z
HENKEL_CAPITAL_2-EQU>enable
HENKEL_CAPITAL_2-EQU#show hw-inventory details
**[short output... due to the limit of the Whosebug text box]**
--More--
            mode is not relevant
HENKEL_CAPITAL_2-EQU#exit
Connection closed by foreign host.

当我看到 --More-- 字符串时,我只是键入 space 栏以获得更多输出,直到最后一个提示 HENKEL_CAPITAL_2-EQU# 显示,然后我只输入 exit 命令。

您的主要问题似乎是您在不同级别交错代码(尤其是那些 sleep 1s!),导致脚本实际上是纯粹的胡言乱语。代码没有崩溃纯属偶然。

重写有问题的部分,同时猜测您需要什么...

# Handle login
expect {
    "user name:" {
        send "$user\r"
        exp_continue; # keep waiting
    }
    "password:" {
        send "$password\r"
        exp_continue; # keep waiting
    }
    ">" {
        # Got a logged-in prompt so stop waiting (by not exp_continuing)
    }
}

# Handle what's going on remotely; note that we've a prompt at this point
# and can use simple challenge/response coding from here on
set prompt "# "
send "enable\r"
expect $prompt
send "terminal length 200\r"
expect $prompt
send "terminal more off\r"
foreach cmd $commands {
    expect $prompt
    send "$cmd\r"
}
expect $prompt
send "exit\r"
expect eof

一旦添加错误处理而不是相信一切正常,这项技术就会变得更有价值,因为 expect 的主要功能之一是能够一次等待几件事。 你通常不需要它,但当你需要它时它非常有价值!

您通常不需要添加 sleep 调用,当您只是发送命令并等待提示返回时不需要。

问题出在这里:

send "show hw-inventory details\r"
expect $prompt
send "exit\r"

set prompt "#"这是很危险的。 show hw-inventory details 的输出确实有字符 #(参见行 part# 82.86S-8609-R6 rev C),所以 expect $prompt 会成功,然后它 send "exit\r" 并且连接关闭。

解决方法:使用更准确的提示字符串,例如:

# '[' needs to be backslash escaped or it'll trigger Tcl's command substitution
set prompt "HENKEL_CAPITAL_2-EQU\[>#]"