How to validate Database password in ksh?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to validate Database password in ksh?
# 1  
Old 01-24-2012
How to validate Database password in ksh?

Hi All,

I want to validate the Production Database password at the time of login through script. If incorrect password entererd by the user, the script will ask again for the password.

Below is the sample of my script...

Code:
#########################
# Unix Code Starts here #
#########################
##------ Pre-Database Connection Starts here ------##
echo 'Please Enter Database Password: '
stty -echo
read db_pass
stty echo
 
###############################
# Database connection started #
###############################
sqlplus -s user/$db_pass@prd3 <<ENDOFSQL
WHENEVER SQLERROR EXIT 1
set serveroutput on size 1000000
set echo off
set feedback off
set heading off
set pages 0
set pagesize 1000
set linesize 300
spool $spool_file
 /* PL/SQL block starts from here */
...

How do I put a check here for database password?
# 2  
Old 01-24-2012
You could try examining the return code from sqlplus:

Code:
sqlplus -s user/$db_pass@prd3 2> /tmp/sql.err <<ENDOFSQL
...
ENDOFSQL
 
return_code=$?
 
if [ $return_code -ne 0 ]
then
    echo "sqlplus failed: error code $return_code"
    echo "Error string is:"
    cat /tmp/sql.err
fi
rm -f /tmp/sql.err

Use an invalid username/password and see what you get back.

If the return code isn't unique for incorrect password (some programs just return 1 for any error found). Try a search for a unique error string in the sql.err file relating to username/password.
# 3  
Old 01-24-2012
Quote:
Originally Posted by Chubler_XL
You could try examining the return code from sqlplus:

Code:
sqlplus -s user/$db_pass@prd3 2> /tmp/sql.err <<ENDOFSQL
...
ENDOFSQL
 
return_code=$?
 
if [ $return_code -ne 0 ]
then
    echo "sqlplus failed: error code $return_code"
    echo "Error string is:"
    cat /tmp/sql.err
fi
rm -f /tmp/sql.err

Use an invalid username/password and see what you get back.

If the return code isn't unique for incorrect password (some programs just return 1 for any error found). Try a search for a unique error string in the sql.err file relating to username/password.

Please check the code and output:
Code:
#! /bin/ksh
#
#########################
# Unix Code Starts here #
#########################
#
LOG_DIR=/app/work/saps/logs
varDate=`date +%d%h%y`
echo 'Please Enter Database Password: '
stty -echo
read db_pass
stty echo
sqlplus -s user/$db_pass@prd3 2> /tmp/sql_today.err <<ENDOFSQL
ENDOFSQL
return_code=$?
echo $return_code
if [ $return_code -ne 0 ]
then
    echo 'sqlplus failed: error code '$return_code
    cat /tmp/sql_today.err >$LOG_DIR/sql_err_${varDate}.txt
else
    echo '\n\033[32mConnected !\033[0m'
fi
#rm -f /tmp/apac_sql_today.err
find $LOG_DIR -name "sql_err_*.txt" -mtime +0 -type f;

The output with correct DB Pass:
Code:
$ksh:/work/saps
ksh:0$ ./test_db_conn
Please Enter Database Password:
0
Connected !

with wrong DB Pass:
Code:
ksh:0$ ./test_db_conn
Please Enter Database Password:
ERROR:
ORA-01017: invalid username/password; logon denied
 
0
Connected !

Still it's going to the else section, output of the $return_code is 0 and it's not creating any log files in LOG Directory.

Please explain.. :S
# 4  
Old 01-24-2012
OK looks like sqlplus is returning 0 regardless. We will need to search for "logon denied" on stdout.

I'm piping output to tee so it still appears on the tty (usefull if the sqlplus session is interactive, and also for debugging). You can change to redirect directly to the file if you don't need the output displayed.

Try this:

Code:
#! /bin/ksh
#
#########################
# Unix Code Starts here #
#########################
#
LOG_DIR=/app/work/saps/logs
varDate=`date +%d%h%y`
echo 'Please Enter Database Password: '
stty -echo
read db_pass
stty echo
( sqlplus -s user/$db_pass@prd3 2>&1 <<ENDOFSQL
ENDOFSQL ) | tee /tmp/sql_today.err 
 
if grep -q "logon denied" /tmp/sql_today.err
then
    echo 'sqlplus failed!'
    cat /tmp/sql_today.err >$LOG_DIR/sql_err_${varDate}.txt
else
    echo '\n\033[32mConnected !\033[0m'
fi
#rm -f /tmp/apac_sql_today.err
find $LOG_DIR -name "sql_err_*.txt" -mtime +0 -type f;

# 5  
Old 01-30-2012
Quote:
Originally Posted by Chubler_XL
Code:
( sqlplus -s user/$db_pass@prd3 2>&1 <<ENDOFSQL
ENDOFSQL ) | tee /tmp/sql_today.err

Hi Chubler,

I'm getting the following err:
Code:
./test_db_conn.ksh[13]: syntax error at line 13 : `<<' unmatched

...

Last edited by saps19; 01-30-2012 at 01:13 AM.. Reason: Tags!
# 6  
Old 01-30-2012
Quote:
Originally Posted by Chubler_XL
OK looks like sqlplus is returning 0 regardless. We will need to search for "logon denied" on stdout.

I'm piping output to tee so it still appears on the tty (usefull if the sqlplus session is interactive, and also for debugging). You can change to redirect directly to the file if you don't need the output displayed.

Try this:

Code:
#! /bin/ksh
#
#########################
# Unix Code Starts here #
#########################
#
LOG_DIR=/app/work/saps/logs
varDate=`date +%d%h%y`
echo 'Please Enter Database Password: '
stty -echo
read db_pass
stty echo
( sqlplus -s user/$db_pass@prd3 2>&1 <<ENDOFSQL
ENDOFSQL ) | tee /tmp/sql_today.err 
 
if grep -q "logon denied" /tmp/sql_today.err
then
    echo 'sqlplus failed!'
    cat /tmp/sql_today.err >$LOG_DIR/sql_err_${varDate}.txt
else
    echo '\n\033[32mConnected !\033[0m'
fi
#rm -f /tmp/apac_sql_today.err
find $LOG_DIR -name "sql_err_*.txt" -mtime +0 -type f;

Try this:

Code:
#! /bin/ksh
#
#########################
# Unix Code Starts here #
#########################
#
LOG_DIR=/app/work/saps/logs
varDate=`date +%d%h%y`
echo 'Please Enter Database Password: '
stty -echo
read db_pass
stty echo
sqlplus -s user/$db_pass@prd3 2>&1 1> /tmp/sql_today.err <<ENDOFSQL
ENDOFSQL
 
if grep -q "logon denied" /tmp/sql_today.err
then
    echo 'sqlplus failed!'
    cat /tmp/sql_today.err >$LOG_DIR/sql_err_${varDate}.txt
else
    echo '\n\033[32mConnected !\033[0m'
fi
#rm -f /tmp/apac_sql_today.err
find $LOG_DIR -name "sql_err_*.txt" -mtime +0 -type f;

---------- Post updated at 03:33 PM ---------- Previous update was at 03:29 PM ----------

Quote:
Originally Posted by saps19
Hi Chubler,
Code:
( sqlplus -s user/$db_pass@prd3 2>&1 <<ENDOFSQL
ENDOFSQL ) | tee /tmp/sql_today.err

I'm getting the following err:
Code:
./test_db_conn.ksh[13]: syntax error at line 13 : `<<' unmatched

...

Or try this:
Code:
(
sqlplus -s user/$db_pass@prd3 2>&1 <<ENDOFSQL
ENDOFSQL
) | tee /tmp/sql_today.err

Make sure the paranthesis after ENDOFSQL {ENDOFSQL )} should be in new line.
# 7  
Old 01-30-2012
Quote:
Originally Posted by knight_eon
Try this:
I have modified my code as below:
Code:
     1  #! /bin/ksh
     2  #
     3  #########################
     4  # Unix Code Starts here #
     5  #########################
     6  #
     7  LOG_DIR=/app/work/saps/logs
     8  varDate=`date +%d%h%y`
     9  echo 'Please Enter Database Password: '
    10  stty -echo
    11  read db_pass
    12  stty echo
    13  result=`sqlplus -s user/$db_pass@uat3 2>&1 <<ENDOFSQL
    14  ENDOFSQL`
    15
    16  #echo $result
    17
    18  if [ "$result" == "" ]
    19  then
    20      echo '\n\033[32mConnected !\033[0m'
    21  else
    22      echo 'sqlplus failed! Invalid username/password. For more details, check the LOG File in ' $LOG_DIR
    23      echo $result >$LOG_DIR/sql_err_${varDate}.txt
    24  fi
    25  #rm -f /tmp/sql_today.err
    26  find $LOG_DIR -name "sql_err_*.txt" -mtime +0 -type f;
    27

It's working fine now...!

Still you guys can review my code and suggest me if anything needs to be added/modified or deleted [Remembering "the Useless use of cat" Smilie].

Last edited by saps19; 01-30-2012 at 08:05 AM.. Reason: Typo
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Setting config database user and password using sed

Hello everybody, I need to modify 200 files using a patern matching, I would like to do it with sed but it's not working with the following syntax: sed -e 's/DATABASE_PASSWORD.*oldpass/DATABASE_PASSWORD__', 'newpass/g' config.php need to find: define("__DATABASE_PASSWORD__", ... (6 Replies)
Discussion started by: dco
6 Replies

2. Shell Programming and Scripting

Any Sample ksh script to validate all the database objects

Hi, I need a sample script to validate all the database objects like 1. table structure(columns, datatypes,key contraints if any) 2. synonyms 3. grants 4. indexes ....etc thank you! (2 Replies)
Discussion started by: srikanth_sagi
2 Replies

3. UNIX for Dummies Questions & Answers

Launch a URL,validate username and password using wget or curl

Hi All, I want to launch "ex: http://gmail.com" from the cmd window and validate the credentials with username and password, is it possible? I have found something like this "wget --http-user=USER' --http-password=PASSWORD http://gmail.com" am new to this and unable to find a solution, i... (0 Replies)
Discussion started by: harsha85
0 Replies

4. Shell Programming and Scripting

Validate xml agaist xsd is ksh

how do i validate xml agaist xsd is ksh? (1 Reply)
Discussion started by: LiorAmitai
1 Replies

5. Shell Programming and Scripting

Database password appearing in script output

Hi there, This is my first post, so as you have probably guessed I am looking for some help. Currently we have close to 1000 ksh scripts operating on HPUX servers that call either isql or bcp to connect to Sybase databases. Problem being that the db passwords are appearing in the job log... (3 Replies)
Discussion started by: kdk_irl
3 Replies

6. Shell Programming and Scripting

passing database password to isql command in shell script

Hi, I need to connect to DB through my shell script. but I dont want to hardcode my db password in the script. Is there a way to do it? Thanks ---------- Post updated at 07:42 PM ---------- Previous update was at 04:54 PM ---------- :(Guys..please help me with this:( (1 Reply)
Discussion started by: agrawal.prachi
1 Replies

7. Shell Programming and Scripting

Sample ksh script for copy the data from excel to database table ?

Hi All, I need to convert the data from excel to database table in sybase. Please provide some sample script.. thanks, Royal. (1 Reply)
Discussion started by: royal9482
1 Replies

8. Programming

userpw.h AIX ( delete entry from the shadow password database )

HI i need to delete an entry in /etc/security/passwd. can't find a way to do it with userpw.h api ( AIX ). the passwd file i delete like this. Write all entrys to passwd file except the one we are removing. can't find any function that works like getspent / getpwent do in AIX userpw api.... (4 Replies)
Discussion started by: nighter
4 Replies

9. UNIX for Dummies Questions & Answers

Password database

I am trying to locate a password database for use at work. I have looked at several open source packages but all that I have found are for a single user and we need to allow multiple user access with view restrictions. Does anyone know of a program that fits these requirements? Thanks... (2 Replies)
Discussion started by: thumper
2 Replies
Login or Register to Ask a Question