loop picks up password for 2 entry...how to avoid that ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting loop picks up password for 2 entry...how to avoid that ?
# 1  
Old 07-20-2011
loop picks up password for 2 entry...how to avoid that ?

hello all, i am trying to find a better to do what i am doing right now...

i have a file called sidlist...which has my database_name and password to the respective database

so something like below.. file is called sidlist and entry is below...

test, abc123
kes12, abcd12
pss, abcd1234
name, pass
kes, pass1

and here is teh actual script....

test.sh

Code:
###########################
#check for the oratab file. to get db name and info
###########################
ORATAB=/etc/oratab

#
# set ORACLE SID and export path to use as client
#
ORACLE_SID=becsdev
ORAENV_ASK=NO
. oraenv
# for loop to get sid info
for ORACLE_SID in `cat /home/sidlist|awk -F, '{print $1}'`
do

echo '###################################################################################' >> test_output.txt
echo 'ORACLE SID IS SET TO' >> test_output.txt
echo $ORACLE_SID  >> test_output.txt

# get password info for each db
pass=`cat /home/sidlist|grep $ORACLE_SID|awk -F, '{print $2}'`
sqlplus "system/$pass@$ORACLE_SID" << EOF >> test_output.txt
@test.sql
EOF
echo '#####################################################################################' >> test_output.txt
done


the script works great....but when it loops to get the password for database name called kes....it picks ups kes12's password as well...a simple test is below....so now my question is how can i avoid that ?? and get the password for kes only and not both ??


Code:
cat /home/sidlist|awk -F, '{print $2}'|grep KES
abcd12
pass1


Last edited by pludi; 07-20-2011 at 11:02 AM..
# 2  
Old 07-20-2011
Get the SID and password at the same time

Try implementing something like this to save a few processes, useless use of cat and get the data you need at the same time, a row at a time (note this is untested)::
Code:
#!/bin/ksh

###########################
#check for the oratab file. to get db name and info
###########################
ORATAB=/etc/oratab
LOGFILE=test_output.txt  # Set the logfile in one place so if it changes you have only one place to edit.

#
# set ORACLE SID and export path to use as client
#
ORACLE_SID=becsdev
ORAENV_ASK=NO
. oraenv
# loop to get sid info
while IFS=',' read ORACLE_SID pass
do
   echo '###################################################################################' >> $LOGFILE
   echo "ORACLE SID IS SET TO $ORACLE_SID" >> $LOGFILE

   sqlplus "system/$pass@$ORACLE_SID" << EOF >> $LOGFILE
  @test.sql
EOF
   echo '#####################################################################################' >> $LOGFILE
done < /home/sidlist

You should read up on using SQL/Plus in a coprocess to save having to start it up once for every row in the sidlist file.

Also be care careful as I believe a ps -ef will show the username and password from the sqlplus command line which is a security issue.

Gary

Last edited by gary_w; 07-20-2011 at 01:32 PM.. Reason: double quotes around the statement echoing ORACLE_SID to the LOGFILE
This User Gave Thanks to gary_w For This Post:
# 3  
Old 07-20-2011
Quote:
Originally Posted by gary_w
Try implementing something like this to save a few processes, useless use of cat and get the data you need at the same time, a row at a time::
Code:
#!/bin/ksh

while IFS=',' read sid pw
do
  print "ORACLE_SID IS SET TO $sid: \c"

  print "Password is: $pw"

done < sidlist

You should read up on using SQL/Plus in a coprocess to save having to start it up once for every row in the sidlist file.

Also be care careful as I believe a ps -ef will show the username and password from the sqlplus command line which is a security issue.

Gary
I see what you mean, i can for sure modify script like that...but what dose the IFS do in while loop ??? is it like a comma seperator ?
# 4  
Old 07-20-2011
Input Field Separator

It's the Input Field Separator so read knows to parse on that character.

P.S. I edited the example to use your code.
This User Gave Thanks to gary_w For This Post:
# 5  
Old 07-20-2011
Quote:
Originally Posted by gary_w
It's the Input Field Separator so read knows to parse on that character.

P.S. I edited the example to use your code.
Thanks gary...
# 6  
Old 07-20-2011
Here it is as a coprocess

I can't test this, but I would be interested in seeing if you like this way with a coprocess better. It is definitely more efficient than starting/closing SQL/Plus for every row in the sidlist file. Try running it with the "time" command and see if it is faster than how you were going to do it before. I have a coprocess example somewhere that I will post that also shows how to retrieve query results into shell variables. I think it would be good info to put out there. Coprocess example: https://www.unix.com/shell-programmin...#post302540429
Code:
#!/bin/ksh

###########################
#check for the oratab file. to get db name and info
###########################
ORATAB=/etc/oratab
LOGFILE=test_output.txt  # Set the logfile in one place so if it changes you have only one place to edit.
output=""                # Output from SQL/Plus goes here.

#
# set ORACLE SID and export path to use as client
#
ORACLE_SID=becsdev
ORAENV_ASK=NO
. oraenv

# Start SQL/Plus as a co-process.
sqlplus -S -NOLOG |&     ## Suppress normal headings and don't logon.

##  Exit SQL/Plus if any of the following signals are received:
##  0=normal exit, 2=interrupt, 3=quit, 9=kill, 15=termination
trap 'print -p "exit"' 0 2 3 9 15

# loop to get sid info
while IFS=',' read ORACLE_SID pass
do
   print '###################################################################################' >> $LOGFILE
   print "ORACLE SID IS SET TO $ORACLE_SID" >> $LOGFILE

   ##  This should have some error handling but for example purposes I assume it will work as expected.
   ##  Usually this is a bad assumption!
   print -p "connect system/${pass}@${ORACLE_SID}"  ## Send the connect command to the coprocess.
   print -p "@test.sql"
   print -p "DONE"
   while read -p output
   do
      if [[ $output == "DONE" ]]; then
        print "DONE" >> $LOGFILE
        break
      else
        # Throw all output into the logfile.
        print $output >> $LOGFILE
      fi
   done
   print '#####################################################################################' >> $LOGFILE
done < /home/sidlist

#  Close SQL/Plus.
print -p "exit"

exit 0


Last edited by gary_w; 07-20-2011 at 03:49 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Set password in bash script without manual entry-Solaris 10

Hi I have a root script which is setting up user and his dirs and so on. After I create user and set up all the necessary I have to manually set user password. I try all possible ways what google find me and nothing works for me. If maybe one of you have a solution for my problem it will be... (1 Reply)
Discussion started by: Jaffakeks
1 Replies

2. Shell Programming and Scripting

Avoid multiple emails being sent - Counter, Loop?

Hello, I have currently coded a bash script below in which it does the following: # Archives compressed file from another location. Basically it moves *.gz files to another location. The script also sends an email whenever a new compressed file is placed. This is the issue that i... (5 Replies)
Discussion started by: Wizard_1979
5 Replies

3. Shell Programming and Scripting

Automatic su password entry script

Before I start, two things. 1) Yes I know it's bad practice and obomination to all things holy, but I'm not a sys admin at JP Morgan, I'm a hobbyist tooling about in a VM, in my pants, at home. 2) If you are just going to flame for even considering hardcoding a root password, thanks, I get... (2 Replies)
Discussion started by: 3therk1ll
2 Replies

4. UNIX Desktop Questions & Answers

How to grep for password file entry

How would I grep for password file entry without using grep 'username' /etc/passwd? perhaps with who? I want to create alias that will find the password file entry regardless of the user who is using it. Thanks (4 Replies)
Discussion started by: alis
4 Replies

5. UNIX for Dummies Questions & Answers

grep for password file entry

How would I grep for password file entry without using grep 'username' /etc/passwd? perhaps with who? I want to create alias that will find the password file entry regardless of the user who is using it. I am trying to get the same exact line from the file entry like: Name : Password : UserID... (7 Replies)
Discussion started by: alis
7 Replies

6. UNIX for Dummies Questions & Answers

Command to delay password entry - putty connection manager

Hi all, putty connection manager is great but when attempting to sudo or ssh to another box via the post login commands it is subject to issues due to network latency (what happens is that pcm enters the password before the unix box is ready to receive it). Is there any clever way I can make... (1 Reply)
Discussion started by: skinnygav
1 Replies

7. Shell Programming and Scripting

automating username / password entry

I have a database that contains a list of server names, and the password for the root user on several servers (100+). I need to verify the passwords for each of the servers in an automated fashion because the database continues to grow. All of the users that I'm going to test are ROOT. I can't... (1 Reply)
Discussion started by: jbeck22
1 Replies

8. Shell Programming and Scripting

How to avoid multiple while loop?

Hi, How can I avoid multiple 'cat while read ....? in my script. In my script, I am taking the inputs from the temp text file and doing the ( cat while read input do ... ... done ) task and deleting later. I know it'll raise the perfomance issue. How to avoid this? (2 Replies)
Discussion started by: sharif
2 Replies

9. Shell Programming and Scripting

in ftp how to avoid username/password

hi all i am very new to unix. we had the below scenior test.bat file which contains ftp -s:logfiles.scr servername logfiles.scr contains username password cd path get file bye We had n number of batch(.bat files). which is having the same kind of scr file pointing... (10 Replies)
Discussion started by: nh1
10 Replies

10. Programming

/etc/shadow update password entry! ( getspent? )

Hi i just whant to update an password entry in /etc/shadow. But dosen't get it to work. Something is wrong! in this code. What i try do do is if user kalle exist in shadow. I whant it to update it's password for just that entry. #include <stdio.h> #include <errno.h> #include <stdlib.h>... (2 Replies)
Discussion started by: nighter
2 Replies
Login or Register to Ask a Question