How I can pass OS value to sql - shell

I am trying to write a shell scripts for changing the db link as everytime password for Dev got change
so I am putting like below.
=======================================
export DEV_PASS=nevert3ll
sqlplus /nolog
connect apps/appspwd#TEST
drop database link TEST_TO_DEV;
create database link TEST_TO_DEV connect to apps identified by ${DEV_PASS} using 'DEV';
exit
EOF
=======================================
but this is treating ${DEV_PASS} a value rather than export value.
Can you advise me how to solve this issue.
Thanks,SM

I think you're missing -s switch in sqlplus:
export DEV_PASS=nevert3ll
sqlplus -s /nolog
connect apps/appspwd#TEST
drop database link TEST_TO_DEV;
create database link TEST_TO_DEV connect to apps identified by $DEV_PASS using 'DEV';
exit
EOF

Related

Ran a SQL*Plus query directly against Pluggable Database

we are starting to work with oracle multitenant architecture, and modify our custom shell commands accordingly. so in the aspect of running an SQLPlus query against the CDB, nothing has really much changed, we are still declaring the Oracle_HOME and Oracle_SID environment variables, and concatenating the actual query and the user and password credentials, like this:
export ORACLE_SID=contdev;export ORACLE_HOME=/oracle/app/oracle/product/19.3;printf "SHOW CON_NAME;"| $ORACLE_HOME/bin/sqlplus -s 'c##devuser/devpassword'
but we are struggling to achieve the same for the PDB, as we don't use tnsnames, we still required to connect with user name and password, with a one liner shell command, not a script. due to our security team policy.
searching on web, we have found that another environment variable is available for the PDB, named: ORACLE_PDB_SID. but when we trying to ran the following command:
export ORACLE_SID=contdev;ORACLE_PDB_SID=contDev1;export ORACLE_HOME=/oracle/app/oracle/product/19.3;printf "SHOW CON_NAME;"| $ORACLE_HOME/bin/sqlplus -s 'c##devuser/devpassword'
We are still getting the output from the root container, and not the PDB:
CON_NAME
------------------------------
CDB$ROOT
Another search on the web, we found that we need use ALTER SESSION SET CONTAINER=contDev1; but it's not clear how to use this, in the above command.
After connecting to the Oracle database, just type show pdbs:
SQL> show pdbs
SQL> alter session set container=<DB_NAME>_PDB;
SQL> show pdbs

SQLPLUS Connection to DB with special characters '#'

I'm trying to connect to my remote db with a user who has an # in his password.
I use sqlplus v19 with an OracleClient and my remote db is an OracleServer v19 aswell. I had no problem during the alter user command on the database :
alter user USER identified by "P#ssword123";
user altered.
Below are the commands I tried to connect with this user :
sqlplus USER/"P#ssword123"#tnsname
sqlplus USER/'"P#ssword123"'#tnsname
sqlplus 'USER/"P#ssword123"'#tnsname
sqlplus USER/\"P#ssword123\"#tnsname
And some variants of those commands.
This always return me the same TNS error :
TNS:could not resolve the connect identifier specified
It looks like these solutions works for old sqlplus versions but I can't figure out how can I solve my problem with this version 19.
Of course, I tried to change the password with non # character and it works but this is not a possible solution in my specific case.
Thank you for all your replies.
I figure it out with your help. I even modified it to make it compatible with a PL/SQL Script which I pass some variables.
The answer :
sqlplus /NOLOG << EOF
connect USER/"P#ssword123"#tnsname
#script_plsql.sql $var1 $var2 $var3
EOF

how to connect to multiple oracle databases of different servers via shell script

I want to connect to multiple (30) oracle databases which resides in different servers (27) via shell script and fetch details from each database. I have a user (test) created on all databases but the details I want to fetch needs sysdba privileges. In my environment, I cannot provide DBA privs to any other user due to limitations. Hence my idea is to connect to each database using sqlplus -s "$user/$password#$tnsentry" and then connect as sysdba to fetch details.
Though I am able to connect to all databases using test, the "connect as sysdba" is getting executed on current database in current server.
My script:
cat tmp/db.par | while read LINE
do
if [ -n "$LINE" ] ; then
tns_entry=$LINE
export tns_entry
sqlplus -s /nolog >> $tmp/query.log <
exit
sqlconn
fi
done
In the tns_entry loop, I have given ABC,DEF,GHI,JKL,MNO databases to get details from and I am running this query from a server where database XYZ resides. I didn't give XYZ in my loop and didn't connect to database but the query results are from XYZ database. Please help me on this.
Thank you!
You should connect as sysdba
add "as sysdba" in the script and try.
conn sys/password#tnsentry as sysdba

Passing variable user,pass,sid in sqlplus comman

I want to pass variables in sqlplus command in my bash script, answer might be given but doesn't work for me.
I tried some thing like this
#!/bin/bash
ssh oracle#10.116.12.26 <<XX
echo Please enter an Oracle Username:
read USERNAME
echo "Please enter the Oracle Username's Password:"
read -s PASS
SID=XE
export conn_str=$USERNAME/$PASS#$SID
sqlplus $conn_str << EOF
select * FROM tabs;
exit
EOF
XX
also tried
sqlplus $USERNAME/$PASS#SID #with option -s and -l
I also find solution like this
but not worked for me.
oracle : 11g
,os : fedora 18
Is there any solution available for this ?
thanks.
You haven't said what actually happens, but I'm guessing you aren't prompted for the credentials, and that maybe it can't find sqlplus. On a Red Hat box this works:
#!/bin/bash
echo Please enter an Oracle Username:
read USERNAME
echo "Please enter the Oracle Username's Password:"
read -s PASS
SID=XE
conn_str=$USERNAME/$PASS#$SID
ssh oracle#10.116.12.26 << EOF
# set these for your specific environment
ORACLE_HOME=<path to ORACLE_HOME>
PATH=$PATH:$ORACLE_HOME/bin # or without .../bin depending on client
TNS_ADMIN=<path to tnsnames.ora directory, if not default>
sqlplus -s /nolog
connect $conn_str
select * FROM user_tables;
exit
EOF
This is collecting the values from the user on the local machine to avoid the 'Pseudo-terminal will not be allocated because stdin is not a terminal' issue.
It is then setting up the Oracle environment once on the remote server - what you need to set depends on which client you're using (particularly whether you're using the instant client, though if you're connecting as oracle that seems unlikely, and you can probably run oraenv).
I've also tweaked it to run SQL*Plus with /nolog and then connect once running, so the credentials aren't exposed in the ps output. And I've switched from the old tabs to the more common user_tables.
The nested heredocs work but aren't necessary; the SQL commands are already being entered in the right place and will be seen by SQL*Plus rather than the remote shell.
Of course, since I don't know what error you were actually seeing this is largely speculation. It would probably be simpler to have the client installed locally and use a SQL*Net connection rather than SSH, but there could be firewall restrictions we don't know about.
Try quoting your here doc delimiter to prevent stale variable expansions on the local machine.
ssh oracle#10.116.12.26 <<'XX'

Should CONNECT work in SQL*PLUS script?

I'd like to run a sqlplus script from a cron job.
I thought I could put a line like:
CONNECT "myuser/mypass#mydb"
within the script and then just execute it with:
sqlplus #myscript
However, when I do so, I get:
SP2-0306: Invalid Option
SP3-0157: unable to CONNECT to ORACLE after 3 attempts, exiting SQL*Plus
Am I misunderstanding the usage of the connect command?
When running CONNECT inside SQL*Plus, remove the quotes:
CONNECT myuser/mypass#mydb
They double quotes are required if you are passing the credentials as an argument to sqlplus:
sqlplus "myuser/mypass#mydb"
, for the shell to parse myuser/mypass#mydb as a single argument if you have spaces in your connection identifier or use additional options like AS SYSDBA.
Use the /NOLOG option.
sqlplus /nolog #myscript
Oracle 11gR2
I ran a .sql file via SQL*Plus connected initially as JOHN. Within the file I connect as SYS and then run a GRANT. See .sql file contents below:
connect sys/password as sysdba
GRANT EXECUTE ON DBMS_CRYPTO TO JOHN;
connect JOHN/DOE
NOTE: I don't recommend keeping the sys/password in a text file btw.
HTH

Resources