Pages

Monday, January 14, 2019

Check Connection from bash

===========================
General
===========================

Check Database Connection details, provided in parameter file (set_vipuser.sql) 
Check connection is done in bash script, returning invalid status if provided credentials were wrong.

in main bash

#--------------------------------------
#Validate parameters in set_vipuser.sql
./chk_connection.sh
status=$?
if [[ $status != 0 ]];then
  echo
  echo Check Connection Settings Failed!
  echo Please Correct Parameter Values in set_vipuser.sql
  echo
fi
#--------------------------------------

in chk_connection.sh

#!/bin/bash

#---------------------------------
#functions
#---------------------------------
report_error(){
  db_user=$1
  echo ==========================================
  echo Error!!!
  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 OK! User $db_user passed Connection Check
}


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

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


export OUTPUT_FILE=/tmp/chk_connections.log

VIPUSER=`grep vipuser ../set_vipuser.sql |awk -F= '{print $2}'`
VIPPASS=`grep vippass ../set_vipuser.sql |awk -F= '{print $2}'`

ADMINUSER=`grep adminUser ../set_vipuser.sql |awk -F= '{print $2}'`
ADMINPASS=`grep adminPass ../set_vipuser.sql |awk -F= '{print $2}'`

CONNECTSTR=`grep "connectstr=" ../set_vipuser.sql |awk -F= '{print $2}'`
CONNECTSTRRMT=`grep "connectstrrmt=" ../set_vipuser.sql |awk -F= '{print $2}'`

rm -f $OUTPUT_FILE
touch $OUTPUT_FILE

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


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


echo Check Connection for: ${ADMINUSER}/${ADMINPASS}@${CONNECTSTR} >> $OUTPUT_FILE
sqlplus -s ${ADMINUSER}/${ADMINPASS}@${CONNECTSTR} >> $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
#----------------------------
connection_ok=`grep "Connection is OK for" $OUTPUT_FILE | grep -i ${VIPUSER} | wc -l`
if [[ $connection_ok != 2 ]];then
  report_error ${VIPUSER}
else
  report_success ${VIPUSER}
fi

connection_ok=`grep "Connection is OK for" $OUTPUT_FILE | grep -i ${ADMINUSER} | wc -l`
if [[ $connection_ok != 1 ]];then
  report_error ${ADMINUSER}
else
  report_success ${ADMINUSER}
fi


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

exit 0

No comments:

Post a Comment