本地运行的expect脚本运行正常,但在通过gitlab CI运行时挂起

时间:2016-09-13 05:01:50

标签: continuous-integration gitlab expect spawn

我正在编写一个expect文件来连接以使用类似下面的脚本来处理与远程设备的交互,并且我遇到了一些问题。主要是,当我单独或交互式地(在跑步者上)运行脚本时,它可以按照我的意愿工作,但是当我在其中一个跑步者上使用Gitlab CI插入测试代码时,它会出现以下问题:

  1. 命令spawn /usr/bin/scp $rsaFile remote:/var/root/id_rsa.pub似乎没有出现在CI日志中 (虽然第一个spawn /usr/bin/ssh remote命令出现了) - 我 怀疑它根本没有被召唤。
  2. 相同的 spawn 命令持续向我提供以下消息,直到脚本在下一个 interact 命令停止执行:"Warning: Permanently added 'fe80::cccc:48ff:fe33:3344%en4' (ECDSA) to the list of known hosts.\r\r\n"
  3. 互动抱怨spawn id exp0 not open(我认为这可能是因为scp命令无法正常工作)
  4. 以下是代码:

    spawn /usr/bin/ssh remote
    
    expect {
        -re ".*assword:.*" {
            exp_send "password\r"; 
            exp_continue
        }
        -re ".*sh.*" {
            exp_send "mount -uw /\r";
            exp_send "nvram -somecommand"
            exp_send "exit\r"
        }
    }
    
    interact
    
    # Copy over the key
    set rsaFile "/var/root/Resources/id_rsa.pub"
    spawn /usr/bin/scp $rsaFile remote:/var/root/id_rsa.pub;
    
    expect {
        # 1&2 were added to the original script for debugging purposes
        -re "^id_rsa.pub.*" { #1: output I'd expect in case of successful scp
            exp_send "exit\r"
        }
        -re ".*assword:.*" {
            exp_send "password\r";
            exp_send "exit\r"
        }
        -re "Warning:.*Warning.*Warning.*" { #2: to debug the expect hanging
            exp_send "exit\r"
        }
    }
    
    interact;
    

    这里是错误消息msg(期望在调试模式下):

    spawn /usr/bin/ssh remote
    
    parent: waiting for sync byte
    
    parent: telling child to go ahead
    
    parent: now unsynchronized from child
    
    spawn: returns {24373}
    
    Gate keeper glob pattern for '.*assword:.*' is '*assword:*'. Activating booster.
    Gate keeper glob pattern for '.*sh.*' is '*sh*'. Activating booster.
    
    expect: does "" (spawn_id exp6) match regular expression ".*assword:.*"? Gate "*assword:*"? gate=no
    
    ".*sh.*"? Gate "*sh*"? gate=no
    
    Warning: Permanently added 'fe80::cccc:48ff:fe33:3344%en4' (ECDSA) to the list of known hosts.
    
    expect: does "Warning: Permanently added 'fe80::cccc:48ff:fe33:3344%en4' (ECDSA) to the list of known hosts.\r\r\n" (spawn_id exp6) match regular expression ".*assword:.*"? Gate "*assword:*"? gate=no
    
    ".*sh.*"? Gate "*sh*"? gate=no
    
    root@fe80::cccc:48ff:fe33:3344%en4's password: 
    
    expect: does "Warning: Permanently added 'fe80::cccc:48ff:fe33:3344%en4' (ECDSA) to the list of known hosts.\r\r\nroot@fe80::cccc:48ff:fe33:3344%en4's password: " (spawn_id exp6) match regular expression ".*assword:.*"? Gate "*assword:*"? gate=yes re=yes
    
    expect: set expect_out(0,string) "Warning: Permanently added 'fe80::cccc:48ff:fe33:3344%en4' (ECDSA) to the list of known hosts.\r\r\nroot@fe80::cccc:48ff:fe33:3344%en4's password: "
    
    expect: set expect_out(spawn_id) "exp6"
    
    expect: set expect_out(buffer) "Warning: Permanently added 'fe80::cccc:48ff:fe33:3344%en4' (ECDSA) to the list of known hosts.\r\r\nroot@fe80::cccc:48ff:fe33:3344%en4's password: "
    
    send: sending "password\r" to { exp6 }
    
    expect: continuing expect
    
    expect: does "" (spawn_id exp6) match regular expression ".*assword:.*"? Gate "*assword:*"? gate=no
    
    ".*sh.*"? Gate "*sh*"? gate=no
    
    expect: does "\r\n" (spawn_id exp6) match regular expression ".*assword:.*"? Gate "*assword:*"? gate=no
    
    ".*sh.*"? Gate "*sh*"? gate=no
    
    expect: does "\r\n\u001b[?1034h" (spawn_id exp6) match regular expression ".*assword:.*"? Gate "*assword:*"? gate=no
    
    ".*sh.*"? Gate "*sh*"? gate=no
    
    -sh-3.2# 
    
    expect: does "\r\n\u001b[?1034h-sh-3.2# " (spawn_id exp6) match regular expression ".*assword:.*"? Gate "*assword:*"? gate=no
    
    ".*sh.*"? Gate "*sh*"? gate=yes re=yes
    
    expect: set expect_out(0,string) "\r\n\u001b[?1034h-sh-3.2# "
    
    expect: set expect_out(spawn_id) "exp6"
    
    expect: set expect_out(buffer) "\r\n\u001b[?1034h-sh-3.2# "
    
    send: sending "mount -uw /\r" to { exp6 }
    
    send: sending "nvram -some-command" to { exp6 }
    
    send: sending "exit\r" to { exp6 }
    
    interact: received eof from spawn_id exp0
    
    parent: waiting for sync byte
    
    parent: telling child to go ahead
    
    parent: now unsynchronized from child
    
    spawn: returns {24376}
    
    Gate keeper glob pattern for '^id_rsa.pub.*' is 'id_rsa?pub*'. Activating booster.
    Gate keeper glob pattern for '.*assword:.*' is '*assword:*'. Activating booster.
    Gate keeper glob pattern for 'Warning:.*Warning.*Warning.*' is ''. Not usable, disabling the performance booster.
    
    expect: does "" (spawn_id exp1) match regular expression "^id_rsa.pub.*"? Gate "id_rsa?pub*"? gate=no
    
    ".*assword:.*"? Gate "*assword:*"? gate=no
    
    "Warning:.*Warning.*Warning.*"? (No Gate, RE only) gate=yes re=no
    
    expect: does "Warning: Permanently added 'fe80::cccc:48ff:fe33:3344%en4' (ECDSA) to the list of known hosts.\r\r\n" (spawn_id exp1) match regular expression "^id_rsa.pub.*"? Gate "id_rsa?pub*"? gate=no
    
    ".*assword:.*"? Gate "*assword:*"? gate=no
    
    "Warning:.*Warning.*Warning.*"? (No Gate, RE only) gate=yes re=no
    
    expect: does "Warning: Permanently added 'fe80::cccc:48ff:fe33:3344%en4' (ECDSA) to the list of known hosts.\r\r\nWarning: Permanently added 'fe80::cccc:48ff:fe33:3344%en4' (ECDSA) to the list of known hosts.\r\n\r\n\r\n" (spawn_id exp1) match regular expression "^id_rsa.pub.*"? Gate "id_rsa?pub*"? gate=no
    
    ".*assword:.*"? Gate "*assword:*"? gate=no
    
    "Warning:.*Warning.*Warning.*"? (No Gate, RE only) gate=yes re=no
    
    expect: does "Warning: Permanently added 'fe80::cccc:48ff:fe33:3344%en4' (ECDSA) to the list of known hosts.\r\r\nWarning: Permanently added 'fe80::cccc:48ff:fe33:3344%en4' (ECDSA) to the list of known hosts.\r\n\r\n\r\nWarning: Permanently added 'fe80::cccc:48ff:fe33:3344%en4' (ECDSA) to the list of known hosts.\r\n\r\n\r\n\r\n\r\n\r\n" (spawn_id exp1) match regular expression "^id_rsa.pub.*"? Gate "id_rsa?pub*"? gate=no
    
    ".*assword:.*"? Gate "*assword:*"? gate=no
    
    "Warning:.*Warning.*Warning.*"? (No Gate, RE only) gate=yes re=yes
    
    expect: set expect_out(0,string) "Warning: Permanently added 'fe80::cccc:48ff:fe33:3344%en4' (ECDSA) to the list of known hosts.\r\r\nWarning: Permanently added 'fe80::cccc:48ff:fe33:3344%en4' (ECDSA) to the list of known hosts.\r\n\r\n\r\nWarning: Permanently added 'fe80::cccc:48ff:fe33:3344%en4' (ECDSA) to the list of known hosts.\r\n\r\n\r\n\r\n\r\n\r\n"
    
    expect: set expect_out(spawn_id) "exp1"
    
    expect: set expect_out(buffer) "Warning: Permanently added 'fe80::cccc:48ff:fe33:3344%en4' (ECDSA) to the list of known hosts.\r\r\nWarning: Permanently added 'fe80::cccc:48ff:fe33:3344%en4' (ECDSA) to the list of known hosts.\r\n\r\n\r\nWarning: Permanently added 'fe80::cccc:48ff:fe33:3344%en4' (ECDSA) to the list of known hosts.\r\n\r\n\r\n\r\n\r\n\r\n"
    
    send: sending "exit\r" to { exp1 }
    
    interact: spawn id exp0 not open
        while executing
    "interact"
        (file "./Resources/Setup.command" line 44)
    
    ERROR: Build failed: exit status 1
    

    任何帮助将不胜感激!提前致谢 顺便说一句,这个问题让我觉得很烦人,因为这是我第一篇关于堆栈溢出的帖子。 :)

1 个答案:

答案 0 :(得分:0)

试试这样:

spawn /usr/bin/ssh remote

expect {
    -re ".*assword:.*" {
        exp_send "password\r";
        exp_continue
    }
    -re ".*-sh-.*" {
        exp_send "mount -uw /; nvram -somecommand; exit\r"
        exp_continue
    }
    eof {}
}
wait

set rsaFile "/var/root/Resources/id_rsa.pub"
spawn /usr/bin/scp $rsaFile remote:/var/root/id_rsa.pub;

expect {
    -re ".*assword:.*" {
        exp_send "password\r";
        exp_continue
    }
    eof {}
}
wait

您的interact可能会失败,因为那时 spawn ed进程可能已经退出,显然它无法与 dead 进行交互 em> process。

对于您的脚本,我的expect eof基本上与您的interact具有相同的效果。他们都等待 spawn ed过程完成。这里的不同之处在于时机。当 spawn ed进程退出时,expect eof只会产生错误。