Expect

期望腳本啟動 ilo2,工作正常但沒有退出

  • December 31, 2021

這是我啟動 ILO2 HP 伺服器的腳本。工作正常(伺服器電源打開),但在 iLO 控制台電源打開後,我只能手動使用 ctrl+c 退出腳本,因此腳本不是完全非互動式的,如何將正確的 ^D 傳遞給從控制台退出?

#!/usr/bin/expect -f

set force_conservative 0  ;# set to 1 to force conservative mode even if
             ;# script wasn't run conservatively originally
if {$force_conservative} {
   set send_slow {1 .1}
   proc send {ignore arg} {
       sleep .1
       exp_send -s -- $arg
   }
}

set timeout -1
set send_slow {1 .1}
spawn csh
match_max 100000
expect -exact "% "
sleep .1
send -s -- "ssh hpilo2\r"
expect -exact "ssh hpilo2\r\r
This is a private system. It is to be used solely by authorized users and may be monitored for all lawful purposes. By accessing this system, you are consenting to such monitoring.\r
\rmyuser@hpilo2's password: "
sleep .1
send -s -- "mypass\r"
expect -exact "\r
User:myuser logged-in to hpilo2.(******* / *********)\r
\r
iLO Advanced **** at  Feb 28 2041\r
Server Name: \r
Server Power: Off\r
\r
</>hpiLO-> "
sleep .1
send -s -- "power on\r"
expect -exact "
\r\r
status=0\r
status_tag=COMMAND COMPLETED\r
Sat Dec 6 12:38:17 2031
\r
\r
\r
Server powering on .......\r
\r
\r
\r
</>hpiLO-> "
sleep .1
send -- "^D"
expect eof

解決方案發現 CTRL+D 在我的 hpilo shell 上什麼也不做,退出 shell 的命令是 ..exit。所以我修改了最新的腳本行

Server Name: \r
Server Power: Off\r
\r
</>hpiLO-> "
sleep .1
send -s -- "power on\r"
sleep 2
send -s -- "^exit\r"

現在完美執行

引用自:https://unix.stackexchange.com/questions/683899