Passing variable user,pass,sid in sqlplus comman - bash

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'

Related

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

sqlplus through SSH failed to resolve tns

I setup an EC2 instance and RDS instance. Then installed oracle instance client on EC2 instance. After that I managed to do sqlplus and connect with database from EC2 instance. To do that I created a tnsnames.ora file and enter the service details of the database.
I can do,
sqlplus user/password#db_alias
But I cannot do, (This gives ERROR: ORA-12154: TNS:could not resolve the connect identifier specified)
ssh username#ip sqlplus user/password#db_alias
Password less ssh also configured. And I'm doing the ssh to the current machine itself. Any thought would be helpful.
Addition to the details. Since I installed oracle instance client, tnsping command is not available. I achieve this by adding following function to the .profile file.
whence tnsping >/dev/null 2>&1 ||
tnsping() {
sqlplus -L -s x/x#$1 </dev/null |
grep ORA- |
(grep -v ORA-01017 || echo OK)
}
This problem could be able to narrow down to the problem in loading environment variables (specifically $TNS_ADMIN). Since the .bashrc has an validation to check the login shell is an interactive one or non-interactive one, the variables which defined at the bottom was not loaded.
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
The reason for tns not to be resolved via ssh is unavailability of $TNS_ADMIN variable. By defining that variables at the beginning of .bashrc, I could able to fix this.
See Also Why does an SSH remote command get fewer environment variables then when run manually?

securely passing password through bash

I am building a bash script for my work to make initial setup and windows-domain join for our Ubuntu machines easy enough for someone who knows nothing about Linux can do it. I have found a lot of people that say that you shouldn't pass passwords through a script but to be efficient, I have to. The script prompts for info and credentials in the beginning and it needs to be able to be left to do it's job without interaction. I can't have it visible through ps when I pass it and I can't have it stored as an unsecured variable. Any suggestions?
If you really must do this, you can read the credentials into variables with read -s early in the script and then pass those values to the prompts. For example:
read -p "Enter your username: " username
read -sp "Enter your password: " password
echo
I included the blank echo because the -s option for read prevents the user's typing from appearing in the terminal, including the new line usually created after a user presses Enter when answering a prompt.
You can then use the $username and $password variables for the rest of your script and the credentials will not have to be stored outside of memory, meaning they will be lost/destroyed after the script completes.
However, note that any programs or utilities which take the credentials as command-line arguments will display those to other users on the machine running the script. For example, if I were to run a MySQL query using this method, I could do:
mysql -u "${username}" -p"${password}" -e "SHOW DATABASES;"
Other users on the machine could see the credentials while that was running with something like ps:
ps -ef | grep mysql
...
watrudoin 29512 29443 0 12:57 pts/4 00:00:00 mysql -u MyUserName -phunter2 -e SHOW DATABASES
You just need to be aware that what you are doing is not necessarily secure, but it seems that you already are.

How I can pass OS value to sql

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

Shell script to automate SonicWall firewall SSH session not working

I'm trying to write a shell script (Bash) to log into a SonicWall firewall device and issue a command to perform automated backups of the devices ruleset. I prefer to do this in Bash but I will accept a python, perl, except, or applescript solution. If it cannot be done in bash please mention that.
Problems:
1.) SSH server on firewall is custom, a user name and password has to be specified after issuing a
$ ssh server.com
so no matter what username you issue e.g.
$ ssh admin#server.com
the SSH server still presents a username and password box after
2.) The SSH server is minimal and I cannot use public-keys
I tried using a here-document but it isn't working and it results in an immediate "connection closed by remote host".
The command I need to execute takes the form of this:
export preferences ftp "ftp.server.com" "user1" "mypassword" "output.exp"
Connecting gives me this:
$ ssh admin#server.com
Copyright (c) 2010 SonicWALL, Inc.
User:
After a username is issued it brings up the password prompt:
User:user1
Password:
I tried a here-document to no avail.
$ ssh server <<+
user1
mypassword
export preferences ftp "ftp.server.com" "user1" "mypassword" "output.exp"
exit
+
Pseudo-terminal will not be allocated because stdin is not a terminal.
Connection to 10.1.1.1 closed by remote host.
I tried using echo to pipe in commands too but that doesn't work either.
Typing the commands in manually works just fine.
Any help on this would be greatly appreciated.
As others have suggested, expect is probably what you want to use here.
Here's a short example of how to work with it from bash to get you started:
login=root
IP=127.0.01
password=helloworld
# +whatever variables you need to use
# Run the expect script from bash
expect_sh=$(expect -c "
spawn ssh $login#$IP
expect \"password:\"
send \"$password\r\"
expect \"#\"
send \"cd $dest_dir\r\"
expect \"#\"
send \"chmod +x $server_side_script $other_script\r\"
expect \"#\"
send \"./$device_side_script\r\"
expect \"#\"
send \"cat results_file\r\"
expect \"#\"
send \"exit\r\"
")
# Output or do something with the results
echo "$expect_sh"
You can automate the ssh session using the original expect, here is a nice article discussing it in detail: http://solar1.net/drupal/automating%20SSH%20with%20expect or the Python module pexepect: http://linux.byexamples.com/archives/346/python-how-to-access-ssh-with-pexpect/
I'm not a BASH expert but i had to do something where interactive password prompts was causing me a problem.
Basically your script needs to wait to be asked to enter login credentials, and pass them when prompted in order to login, once logged in you can issue the command.
I recommend looking at spawning "expect" sessions. Basically in your script you use expect to basically say "i expect to see password: in the response, when i do, i need to pass in the following data".
Here's the wiki page which helps explain it http://en.wikipedia.org/wiki/Expect
and if you google around you will find lots of help.
that didn't work for me.
I had to pass the variables to the script at launch.
Example launch script login2.sh, with three arguments:
-bash-4.1$ ./login2.sh Jan2**** HIE_SUPER 10.244.112.182

Resources