期待来自Nagios的脚本错误,但是来自cli的好

时间:2013-04-10 21:43:19

标签: expect nagios

我现在正在学习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
-----------------------

任何人都能提供一些关于这里出了什么问题的线索吗? 在此先感谢!!

0 个答案:

没有答案