0

我需要在 Oracle 应用服务器上配置一项服务来测试与数据库的连接性,然后发送有关状态的电子邮件通知。谁能帮助我。

谢谢,

4

1 回答 1

0

这是一个选项:

  • 您感兴趣的 TNSPING 数据库
  • 评估结果(你对最后一行感兴趣)
  • 通过电子邮件发送结果

几点说明:

  • tnsping %1 > ping_result.txtpings 数据库作为参数 (%1) 传递并将结果存储到 .txt 文件中
  • 脚本的中间部分是由@Aacini 编写的修改代码(你会看到一个链接)
  • 最后,通过邮件发送结果bmail(您可以使用任何其他命令行 SMTP 邮件程序;或者,谷歌“bmail 下载” - 它是免费的)

给你

rem PING_DB.BAT

@echo off
setlocal EnableDelayedExpansion

tnsping %1 > ping_result.txt

rem Littlefoot slightly adjusted code: https://stackoverflow.com/questions/27416341/how-can-i-read-the-last-2-lines-of-a-file-in-batch-script
rem Tail command in pure Batch, version 2: Tail.bat filename numOfLines
rem Antonio Perez Ayala

set /A firstTail=1, lastTail=0
for /F "delims=" %%a in (ping_result.txt) do (
   set /A lastTail+=1, lines=lastTail-firstTail+1
   set "lastLine[!lastTail!]=%%a"
   if !lines! gtr 1 (
      set "lastLine[!firstTail!]="
      set /A firstTail+=1
   )
)
for /L %%i in (%firstTail%,1,%lastTail%) do set result=!lastLine[%%i]!

rem end of LF's adjustment

echo %result%

echo off
bmail -s smtp.server.name -f tnspinger@me.com -t my_real_mail@gmail.com -a "%result%" -c

bmail的选项:

Command Line SMTP Emailer V1.07
Copyright(C) 2002-2004 Craig.Peacock@beyondlogic.org
Date: Wed, 20 Jun 2018 10:08:15 +0200
Usage: bmail [options]
        -s    SMTP Server Name
        -p    SMTP Port Number (optional, defaults to 25)
        -t    To: Address
        -f    From: Address
        -b    Text Body of Message (optional)
        -h    Generate Headers
        -a    Subject (optional)
        -m    Filename (optional) Use file as Body of Message
        -c    Prefix above file with CR/LF to separate body from header
        -d    Debug (Show all mail server communications)

测试(我已经删除了 bmail 的输出):

M:\>ping_db xe
TNS-12541: TNS:no listener     --> this is being sent by e-mail

M:\>ping_db orcl
OK (10 msec)                   --> this is being sent by e-mail
于 2018-06-20T08:15:24.863 回答