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

Posted

技术标签:

【中文标题】Linux 期望脚本有时以“连接被外国主机关闭”结尾,这是期望脚本还是设备问题?【英文标题】:Linux expect script sometimes ending with `Connection closed by foreign host`, is this expect script or device issue? 【发布时间】:2020-05-30 18:45:31 【问题描述】:

我正在运行 CentOS 7.6

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

这是来自 repos 的最新版本的期望。

$ 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 -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.

正如您在上面看到的,我得到的错误是 Connection closed by foreign host 我无法获得 show hw-inventory details 命令的完整输出 ,不确定输出的大小是否会影响这一点。

作为我知道的用于调试此期望脚本的唯一资源是添加 -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"

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

如果您愿意,我们可以开一个screen 会议,我们可以一起解决这个问题,此时我对这个奇怪的问题感到绝望,不知道如何继续前进。

这是完整的预期输出:

$ 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 *** text box]**
--More--
            mode is not relevant
HENKEL_CAPITAL_2-EQU#exit
Connection closed by foreign host.

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

【问题讨论】:

看看sexpect (Expect for Shells),您可以使用它来编写仅使用shell代码Expect脚本。 我已经重新格式化了我的代码并使其更易于阅读,以专注于我正在使用没有身份验证的设备的问题,因此我们可以专注于关闭连接的原因 连接关闭由外国主持人。并确认我们是否可以做些什么。 【参考方案1】:

问题出在这里:

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\[>#]"

【讨论】:

这很好,在我编辑提示后它工作了,现在的问题是如何将它设置为动态的,因为主机名从设备更改为设备。我想我总能在第一个 > 巧合时得到设备名称。 ``` HENKEL_CAPITAL_2-EQU>enable ``` 这是我目前的提示。 ``` grep '设置提示符' runCommands.exp 设置提示符“HENKEL_CAPITAL_2-EQU#” ```【参考方案2】:

您的主要问题似乎是您在不同级别交错代码(尤其是那些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 调用,而不是当您只是发送命令并等待提示返回时。

【讨论】:

嘿@Donal Fellows 我已经听从了你的建议,但我仍然遇到同样的错误,还有什么我应该尝试的吗?

以上是关于Linux 期望脚本有时以“连接被外国主机关闭”结尾,这是期望脚本还是设备问题?的主要内容,如果未能解决你的问题,请参考以下文章

期望脚本不适用于多个密码请求

通过像 Unix/Linux 中的期望脚本一样通过 c++ 提供登录名和密码

如何编写条件期望脚本以根据服务器接受不同的密码集?

LightOJ 1342 Aladdin and the Magical Sticks 期望(结论题)

期望脚本自动进行 telnet 登录

期望脚本产生 sftp 中的 EOF