Pages

Sunday, May 8, 2022

check connection bash script

==================
General
==================
Bash script that takes 3 parameters, and returns 1 if connection was bad or zero if connection was good.

input:
- user
- password
- connection string

output
number 1/0
0 - OK
1 - Bad

#!/bin/bash

VIPUSER=$1
VIPPASS=$2
DB_INST=$3

#---------------------------------
#functions
#---------------------------------
report_error(){
  db_user=$1
  echo ==========================================
  echo Error!!!
  echo User $db_user Connection Check........Failed
  echo Connection Error for User $db_user
  echo ERROR: ORA-01017: invalid username/password. logon denied
  echo Exiting script!
  echo ==========================================
  exit 1
}

report_success(){
  db_user=$1
  echo "User $db_user Connection Check........Passed"
}

#---------------------------------
#main
#---------------------------------

echo ==========================================
echo Check Connections Start
echo ==========================================

export OUTPUT_FILE=./chk_connections.log

rm -f $OUTPUT_FILE 2>/dev/null
touch $OUTPUT_FILE

echo "Check Connection for:  ${VIPUSER}"
sqlplus -s ${VIPUSER}/${VIPPASS}@${DB_INST} >> $OUTPUT_FILE << EOD
set serveroutput on
set heading off linesize 130 pagesize 1000  feedback off
SELECT 'Connection is OK for '||USER FROM DUAL;
EOD


#----------------------------
# Analyze Connection Results
#----------------------------
#echo "OUTPUT_FILE=$OUTPUT_FILE"
#cat $OUTPUT_FILE
connection_ok=`grep "Connection is OK for" $OUTPUT_FILE | grep -iw ${VIPUSER} | wc -l`

if [[ $connection_ok != 1 ]];then
  report_error ${VIPUSER}
  ret_status=1
else
  report_success ${VIPUSER}
  ret_status=0
fi

rm -f $OUTPUT_FILE

echo ==========================================
echo Check Connections Finish
echo ==========================================

exit $ret_status

No comments:

Post a Comment