I need to configure a service to on Oracle application server to test connectivity with database and then send an email notification about status. Can anyone help me please.
Thanks,
Here's one option:
TNSPING database you're interested in
evaluate the result (you're interested in the last line)
send the result by e-mail
A few remarks:
tnsping %1 > ping_result.txt pings database passed as a parameter (%1) and stores the result into a .txt file
middle part of a script is modified code written by #Aacini (you'll see a link)
finally, mailing the result is done by bmail (you can use any other command line SMTP mailer; or, Google for "bmail download" - it is free)
Here you go:
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's options:
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)
Testing (I've removed bmail's output):
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
Related
Have tried different approaches to make the SSMs job working with SCP command with perl script. but the job is going into loop with out having a result.
PS : The script is working fine with running from command prompt directly.
command used in the perl script:-
$Command = "scp -i D:\File1\RS2\DataFeed\Code\PrivateKey.ppk -s $InternalFile admin#sftp.world.com:$VendorName/$DestFileName";
system command used in perl
system($command);
While running the command directly from windows cmd it is correctly placing file to the SFTP. but while running this perl script from ssms agent job it seems not working and the job is keep running without any results.
Any possible leads to the actual errors will be much appreciated
Detailed Steps :
Job in SSMS :
Step :
DataFeed.cmd
%_Debug% echo off
cd /d %0\..
pushd .
setlocal
rem -----------------------------------------------------------------
rem Localize environment
rem -----------------------------------------------------------------
if exist DataFeed_Environment.cmd (
call DataFeed_Environment.cmd
) else (
echo DataFeed_Environment.cmd not found!!!
echo
goto CmdUsage
)
rem -----------------------------------------------------------------
rem Run perl package
rem -----------------------------------------------------------------
C:\Perl\bin\perl.exe DataFeedProd1.pl
if %ERRORLEVEL% NEQ 0 goto ErrorExit
goto Exit
rem -----------------------------------------------------
rem Command Usage
rem -----------------------------------------------------
:CmdUsage
Echo ---------------------------------------------------------------------
echo.
echo DataFeed.cmd
echo Wraps the call to DataFeed.pl,
echo mails log upon errors.
echo.
echo Usage:
echo DataFeed.cmd
echo.
echo ----------------------------------
rem endlocal
rem popd
rem exit 1
rem -----------------------------------------------------------------
rem Error exit
rem -----------------------------------------------------------------
:ErrorExit
echo DataFeedProd1.pl failed !!!
echo
rem endlocal
rem popd
rem exit 1
rem -----------------------------------------------------------------
rem Exit
rem -----------------------------------------------------------------
rem endlocal
rem popd
:Exit
rem exit 0
sub CopyDataFeedFileToSftp{
my ($DataFeedFileInternal, $DataFeedVendorName,$DataFeedFileName) = #_;
my($DestFileName)=$DataFeedFileName.".zip";
my($Command);
my($RetValue) = 1;
$Command = "C:\\Users\\hprasu\\Downloads\\OpenSSH-Win64\\scp.exe -i D:\\File1\\RS2\\DataFeed\\Code\\PrivateKey.ppk -s $DataFeedFileInternal a_Tne\#nasftp\.egencia.com:$DataFeedVendorName/$DestFileName";
$RetVal = &CallSystem($Command);
if ($RetVal == 0) {
&AppendFileToLog($TempFile);
&ErrorExit("Unable to copy data feed file using SCP command:\n".$Command);
}
}
The above perl method is executing the System command
You should:
have full path to perl.exe and your perl script in job's command
escape all special characters in interpolated strings for Perl and
use full path for scp command since
operating system don't know where scp.exe is located (until it in the $PATH):
check filesystem permissions for all files in the command and perl script. Job should has access those files.
So command would be
$Command = "full_path\\scp.exe -i D:\\File1\\RS2\\DataFeed\\Code\\PrivateKey.ppk -s $InternalFile admin\#sftp.world.com:$VendorName/$DestFileName";
Read this:
https://www.geeksforgeeks.org/perl-quoted-interpolated-and-escaped-strings/
I am trying to create a batch file to run start some micro services and database
1 FOR /F "tokens=4 delims= " %%P IN ('netstat -a -n -o ^| findstr :1000') DO #ECHO TaskKill.exe /PID %%P
2 FOR /F "tokens=4 delims= " %%P IN ('netstat -a -n -o ^| findstr :1001') DO #ECHO TaskKill.exe /PID %%P
3 FOR /F "tokens=4 delims= " %%P IN ('netstat -a -n -o ^| findstr :5432') DO #ECHO TaskKill.exe /PID %%P
4 start cd "C:\Program Files\PostgreSQL\10\bin\" & pg_ctl.exe -D "c:\Program Files\PostgreSQL\10\data" start
#REM to start service
5 start javaw -jar -Dserver.port=1000 text-annotation-tool-1.0-SNAPSHOT.jar
Line 1 to 3 and Line 5 execute correctly when executed by commenting line 4.
Line 4 is to start a Postgres server in a new prompt (beacuse of the Dir change). I think the problem is with the way I have used quotes. The 'start' at the beginning and ending of line 4 serve different purpose.
Also, if I execute line 4 in different prompt, How do I close the prompt after execution (nohup equivalent)
There are two errors: You can't "pass" cd to the start command. And start has the quirk to interpret the first quoted parameter as the new window's title. So start "C:\Program Files\PostgreSQL\10\bin\" ... wouldn't work, you need to supply a dummy window title. The & also seems wrong
So you need:
start "Postgres Server" "C:\Program Files\PostgreSQL\10\bin\pg_ctl.exe" -D "c:\Program Files\PostgreSQL\10\data" start
As the full path to pg_ctl.exe is supplied, there is no need for a cd.
But if you want to define the default directory for the new process, you have to use the /D parameter:
start "Postgres Server" /D "C:\Program Files\PostgreSQL\10\bin" pg_ctl.exe -D "c:\Program Files\PostgreSQL\10\data" start
Unrelated, but: putting the Postgres data directory into c:\Program Files\ is a very bad idea. That directory has special permissions for a purpose. You should use %ProgramData% or %AppData% instead
I want to remove/trim the leading/left white space which are as a result of me hiding headers and footers from my PostgreSQL query using Windows batch. I am not sure whether these are white spaces or tabs.
My SQL query:
psql -d databasename -p portname -U username -t -f filename -o "C:\text.txt"
I am not aware of any other way to do this since my SQL is a multi line query and I am not sure if we can do this using -c.
Previous the result was something like this:
After removing the header:
So as you can see there is a white space here and I want to remove it.
Can someone please help me with this?
Have a look at the -t and -A psql parameters:
-t removes headers and footers from the results
-A switches off aligned mode (which is most likely where your whitespace is coming from - alignment into columns).
So the command should look something like the following:
psql -d databasename -p portname -U username -t -A -f filename -o "C:\text.txt"
So, basically, you shouldn't need to modify the resulting file - you can modify your psql command to get results in a format you want.
Here is an hybrid script (batch\vbscript) to trim a string left and right :
#echo off
Set "VAR= abc#abc.com "
echo My Variable before the Trim Function VAR="%VAR%"
Call :Trim "%VAR%"
echo(
echo My Variable after the Trim Function VAR="%VAR%"
pause>nul & exit
::*************************************************************************
:Trim <String>
(
echo Wscript.echo Trim("%~1"^)
)>"%tmp%\%~n0.vbs"
for /f "delims=" %%a in ('Cscript /nologo "%tmp%\%~n0.vbs"') do (
set "VAR=%%a"
)
exit /b
::**************************************************************************
i'm trying to read files and load them all csv files, and once done move the files to some other location :
cd E:\data\
for /f %%a in (’dir /b filename*.CSV) do (
psql -U postgre -W password -c "COPY INTO LA from %%a" zipcodes
mv %%a E:\data\bc\
)
but it gives me following error:
E:\data>for /F %a in (ΓÇÖdir /b filename*.CSV) do (
psql -U postgre -W password-c "COPY INTO LA from %a" zipcodes
mv %a E:\data\bc\
)
The system cannot find the file ΓÇÖdir.
thanks for help
#echo off
setlocal enableextensions disabledelayedexpansion
set "psql=c:\wherever\psqlIs\psql.exe"
pushd "e:\data" && (
for %%a in ("filename*.CSV") do (
"%psql%" -U postgre -W password -c "COPY INTO LA from %%~a" zipcodes
move "%%~a" "E:\data\bc\"
)
popd
)
for /f is intended for file/string processing. To iterate over a set of files use a simple for
Anyway, the problem in your code are the opening single quote (as Alex K has pointed), the missing closing quote and the non existing mv command in windows that should be move
edited it seems there are problems with file loading. psql copy command indicates it is better to use full path names (with backslashes doubled), so
#echo off
setlocal enableextensions enabledelayedexpansion
set "psql=c:\wherever\psqlIs\psql.exe"
pushd "e:\data" && (
for %%a in ("filename*.CSV") do (
set "file=%%~fa"
"%psql%" -U postgre -W password -c "COPY INTO LA from E'!file:\=\\!'" zipcodes
move "%%~a" "E:\data\bc\"
)
popd
)
Is there an easy way to grab the IP address from my service provider and put it into a variable via command prompt? Something like the following:
SET hostIP = nslookup \address
ECHO %hostIP%
Or
SET hostIP = ipconfig \address
ECHO %hostIP%
for /f "skip=1 tokens=2 delims=: " %f in ('nslookup %COMPUTERNAME% ^| find /i "Address"') do echo %f
The answer by Arun is good but I found that using NSLOOKUP generates a rogue comma after the hostname when more than one IP is assigned/associated with a given host.
However, I did find another way that resolves the (first assigned) IP from a given host name and doesn't generate the rogue comma - it uses PING. Very fast, very reliable.
for /f "tokens=2 delims=[]" %f in ('ping -4 -n 1 %COMPUTERNAME% ^| find /i "pinging"') do echo IP=%f
It generates a simple IPv4 address for the hostname into the variable IP. If you then do an ECHO %IP% it will show you the IP like:
IP=192.168.1.2
Of course, in batch scripts, you're going to need to replace the single %f with %%f. Note the carat ("^") in front of the pipe ("|") symbol, which is required in batch scripts so they don't interpret the pipe, and instead pipes the results of the ping statement to the find statement.
If you could use bash, (as in cygwin) this would easily be done using back-ticks to execute anything you want in your SET hostIP line.
As in
export hostIP = `curl 'http://whatsmyip.net' | grep '<title' | awk '{print $8}' | sed -e 's:<.*::g'`
Try a batch like this to set environment variables:
ipconfig > ipconfig.out
setx IPADDR /f ipconfig.out /a 7,13
setx IPADDR /f ipconfig.out /a 7,14
setx IPMASK /f ipconfig.out /a 8,14
Exit the command prompt and open a new one. Use SET and look for IPADDR and IPMASK, which are now persistent. To update the variables, you would have to rerun the batch and exit the command prompt. The different coordinates shown account for differences in the IPCONFIG output for Windows 2003 vs Windows 2008 (should work on XP/7 in the same way). Only a found value is written, so the line that fails does no harm as long as nothing is found. Add the gateway with:
setx IPGATE /f ipconfig.out /a 9,12