1

我现在正在学习 Expect 以尝试在 Nagios Core 下对远程系统进行自动检查,并且虽然脚本从“nagios”用户帐户(这是 Nagios 运行其命令的地方)完美运行,但 Nagios 下的脚本错误跑。事实上,从命令行运行的脚本可以立即运行,没有太多戏剧性 - 但现在我花了 3 天时间试图弄清楚 Nagios 下出了什么问题。

我已经确定 CLI 版本可以看到send "aaa test-server...命令的响应,但 Nagios 运行没有,只是超时。我已将内容写入日志文件以尝试确定我做错了什么,但我只是看不到它。(我已经禁用了对 Nagios 的响应,因为这会使它感到困惑,但它们会在脚本中看到和使用。)

我试图自动化的序列:

nagios:~# ssh accountname@192.168.1.20
accountname@192.168.1.20's password:
Last login: Wed Mar 27 03:43:03 2013 from 192.168.1.14
(Aruba3400) >en
Password:***********
(Aruba3400) #aaa test-server mschapv2 tester account1 password1
Authentication Successful
(Aruba3400) #exit
(Aruba3400) >exitConnection closed by foreign host.
Connection to 192.168.1.20 closed.

脚本:

#!/usr/bin/expect

# turn on logging output
log_user 0
log_file /tmp/output.txt
# set Variables
set password SomeThingOrOther
set useracct accountname
set ipaddr 192.168.1.20
set timeout 9

match_max -d 10000
# now connect to remote UNIX box (ipaddr) with given script to execute
spawn -noecho /usr/bin/ssh $useracct@$ipaddr
# Look for password prompt
expect "assword:"
# Send password aka $password
send "$password\n"
# send blank line (\r) to make sure we're at the prompt
expect "Aruba3400"
send "\r"
#send_log "log: 1st password entered.\n"

# Look for password prompt again, send Enable req
expect "Aruba3400"
send "en\r"
expect "assword:"
send "$password\r"
expect "(Aruba3400) #"
# send blank line (\r) to make sure we're at the prompt
send "\r"
expect "#"

send_log "log: Sent auth command.\n"
send "aaa test-server mschapv2 tester account1 password1\r"

# at next line, nagios run times out; command line runs fine
expect -timeout 9 "Successful"
# printout some debugs to logs whether it works or times out
send_log "log 0: $expect_out(0,string)\n"
send_log "log x: $expect_out(buffer)\n"

#---stock exit here so I can see some output, fix it later----
set TIME [exec date +%H:%M:%S]
log_user 1
send_user "Time: $TIME\n"
send_log "-----------------------\n"
exit 0

日志文件的输出:

-----------------------
log: Sent auth command.
log 0: Successful
log x: aaa test-server mschapv2 tester account1 password1

Authentication Successful     <-- this is from the cli (works!)
Time: 16:00:02
-----------------------
log: Sent auth command.
log 0: #
log x:
(Aruba3400) #                 <-- this is under Nagios (timeout)
Time: 16:00:29
-----------------------

谁能给我一些关于这里出了什么问题的线索?提前致谢!!

4

0 回答 0