我正在编写一个expect文件来连接以使用类似下面的脚本来处理与远程设备的交互,并且我遇到了一些问题。主要是,当我单独或交互式地(在跑步者上)运行脚本时,它可以按照我的意愿工作,但是当我在其中一个跑步者上使用Gitlab CI插入测试代码时,它会出现以下问题:
spawn /usr/bin/scp $rsaFile
remote:/var/root/id_rsa.pub
似乎没有出现在CI日志中
(虽然第一个spawn /usr/bin/ssh remote
命令出现了) - 我
怀疑它根本没有被召唤。"Warning: Permanently added 'fe80::cccc:48ff:fe33:3344%en4' (ECDSA) to the list of known hosts.\r\r\n"
spawn id exp0 not open
(我认为这可能是因为scp命令无法正常工作)以下是代码:
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
任何帮助将不胜感激!提前致谢 顺便说一句,这个问题让我觉得很烦人,因为这是我第一篇关于堆栈溢出的帖子。 :)
答案 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
只会产生错误。