我有一部分期望脚本,它连接到远程服务器,并运行命令/opt/arne/bin/import.sh -f /home/janko496/moshell/test.xml -val:rall
。所以基本上它做得很好,但我需要的是捕获一些可能在此命令-vall-rall
之后发生的错误然后退出。
以下是运行时脚本的日志:
janko496@rtwauas1o> perl ex
*************************************************************
* /// START /// *
*************************************************************
spawn ssh mashost
Password:
Your password will expire in 15 days.
Last login: Mon Jan 26 09:51:52 2015 from rtwauas1o
janko496@rtwaadmin1o> cd /home/janko496/moshell
janko496@rtwaadmin1o> /opt/arne/bin/import.sh -f /home/janko496/test.xml -val:rall
Logging to file /var/opt/arne/ARNE_Import_Log.2015-01-26_09:54:12
-------------------------------------------------------------------------------
MUG does not support the delete upgradation operation.
-------------------------------------------------------------------------------
Validating XML File.....
Delete XML files are only validated for DTD and limited semantic errors.....
Finished validating file /home/janko496/test.xml
There were 0 errors reported during validation
janko496@rtwaadmin1o> /opt/arne/bin/import.sh -f /home/janko496/test.xml -import
Logging to file /var/opt/arne/ARNE_Import_Log.2015-01-26_09:54:23
-------------------------------------------------------------------------------
MUG does not support the delete upgradation operation.
-------------------------------------------------------------------------------
Starting Import..
Progress :7%:Validating against mandatory rules for 'siu064842'
Progress :15%:Adjusting with NEC for 'siu064842'.
Progress :23%:Deleted Target Information.
Progress :30%:Deleting Object 'SubNetwork=NL1,SubNetwork=EHWOUR_SIU,ManagedElement=siu064842'.
Progress :38%:Action Committed
Progress :46%:Object fully deleted.
Progress :53%:Adjusting with NEC for 'siu064842'.
Progress :61%:Transaction committed, updating Area.
Progress :100%:Temporary Area Deleted.
Import Finished.
No Errors Reported.
这是脚本:
set strname There were 0 errors reported during validation
spawn ssh mashost
#exp_internal 1
expect {
"assword" {send "$PASSWORD\r"}
}
set timeout 100
expect "janko496@rtwaadmin1o"
send -- "cd /home/janko496/moshell\r"
expect "janko496@rtwaadmin1o"
send -- "/opt/arne/bin/import.sh -f /home/janko496/moshell/test.xml -val:rall \r"
if { [string compare expect_out(0,string) strname ] != 1 } {
send "exit\r";
}
send -- "/opt/ericsson/arne/bin/import.sh -f /home/janko496/test.xml -import \r"
expect "janko496@rtwaadmin1o"
所以我想抓住这个字符串"There were 0 errors reported during validation
并继续或退出,如果有错误的话。
答案 0 :(得分:1)
你希望expect
在:
send -- "/opt/arne/bin/import.sh -f /home/janko496/moshell/test.xml -val:rall \r"
您正在等待的两件事是指示成功的模式和指示失败的模式。请注意,使用正则表达式模式(-re
)可以对此有很大帮助,正则表达式应该(通常)放在大括号中:
expect {
"There were 0 errors reported during validation" {
puts "Yay, validation succeeded"
# You don't really need to put very much in here...
}
-re {There were (\d+) errors reported during validation} {
puts "Found $expect_out(1,string) errors in validation"
send "exit\r"
after 100
close
exit
}
}