sqlplus through SSH failed to resolve tns - oracle

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?

Related

Run a unix shell script present on another server from a differnt server

I have server 1 where shell script1 is placed. I want to execute shell script 2 present on another server from the shell script1 itself which is on server 1.
This is what I am trying to do.
ftp -nv $hostname << END
quote user $UID
quote pass $pwd
binary
put $source_file
quit
END
rc=$?
echo "Return Code:$rc"
if [ $rc == 0 ]
then
**execute My_port script (present on remote server) which works on the ftpd file and do something**.
else
exit 1
fi
hope I make the requirement clear.
Does anyone have any idea on that?
To run any program remotely, you need (on the remote machine) some server program capable of that (and on the local machine, some client program compatible with it).
Usually, you'll use ssh (read its man page ssh(1)). Perhaps you'll have some openssh server. You'll find many SSH tutorials on the web (you probably want to use a public key to avoid needing to type a password).
There is also rsh, but without encryption so insecure. You probably don't want it, and it would be foolish to use it on anything else than a trusted local network.
So your shell script could contain something like ssh remotehost My_port script arguments...
Be also aware of the PATH variable (it is different on the local machine and on the remote ones, in general). It does matter, notably its remote settings, for commands (and remote commands). You might need to give the full path of scripts (e.g. ssh remotehost /home/remoteuser/bin/myport.sh ...) If the remote PATH setting don't have the directory containing the script. You could use ssh remotehost 'echo $PATH' to query the remote PATH setting.

How To Obtain Remote Client SSH IP From Sudo'd Script

I'm trying to create a small setup script which requires the user to enter their own IP address... I'm trying to make the default IP address be that of the connected SSH session.
When I run ${SSH_CLIENT%% *} in the command line, it shows 99.99.99.99: command not found and when I do echo ${SSH_CLIENT%% *} it shows just 99.99.99.99 (where 99.99.99.99 is my actual WAN IP)...
However, the following code (when ran in a bash script) shows
[6/9] Enter your public IP address. (eg. ) :
declare CONNECTED_IP=${SSH_CLIENT%% *}
read -e -p "[6/9] Enter your public IP address. (eg. $CONNECTED_IP) : " ipAddress
[ -z "$ipAddress" ] && ipAddress=$CONNECTED_IP
Update 1
Upon further review, it does work... except for when I run the script with sudo. Could someone explain this behavior to me and is there a work-around?
Update 2
So, it's been explained to be that this behavior is due to the fact that environment variables are not preserved when switching users (sudo) and that I could use the -E flag when executing the script to preserve the environment.
Unfortunately though, this is a script that will be shared and I therefore cannot ensure that the user executes the script with the -E flag. Furthermore, I don't even know if they'll use sudo at all.
That said, I'm looking for a consistent way to obtain the IP address of the user connected via SSH.
The reason the variable doesn't show when using sudo is that it doesn't automatically preserve the environment when you switch from another user. You can use the -E sudo option to preserve the current environment.
$ sudo -E ./script.sh
Should show the ip as expected.
-E, --preserve-env
Indicates to the security policy that the user wishes to preserve
their existing environment variables. The security policy may
return an error if the user does not have permission to preserve
the environment.

Messed up sed syntactics in hadoop startup script after reinstalling JVM

i'm trying to run 3 node Hadoop cluster on Windows Azure cloud. I've gone through configuration, and test launch. Everything look fine, however, as i used to use OpedJDK which is not recommended as VM for Hadoop according to what i read, i decide to replace it with Oracle Server JVM. Removed old installation of java with Yum, along with all java folders in /usr/lib, installed most recent version of Oracle JVM, updated PATH and JAVA_HOME variables; however, now on launch i getting following masseges:
sed: -e expression #1, char 6: unknown option to `s'
64-Bit: ssh: Could not resolve hostname 64-Bit: Name or service not known
HotSpot(TM): ssh: Could not resolve hostname HotSpot(TM): Name or service not known
Server: ssh: Could not resolve hostname Server: Name or service not known
VM: ssh: Could not resolve hostname VM: Name or service not known
e.t.c. (in total about 20-30 strings with words which should not have anything in common with hostnames)
For me it looks like it's trying to pass part of code as Hostname because of incorrect usage of sed in start up script:
if [ "$HADOOP_SLAVE_NAMES" != '' ] ; then
SLAVE_NAMES=$HADOOP_SLAVE_NAMES
else
SLAVE_FILE=${HADOOP_SLAVES:-${HADOOP_CONF_DIR}/slaves}
SLAVE_NAMES=$(cat "$SLAVE_FILE" | sed 's/#.*$//;/^$/d')
fi
# start the daemons
for slave in $SLAVE_NAMES ; do
ssh $HADOOP_SSH_OPTS $slave $"${#// /\\ }" \
2>&1 | sed "s/^/$slave: /" &
if [ "$HADOOP_SLAVE_SLEEP" != "" ]; then
sleep $HADOOP_SLAVE_SLEEP
fi
done
Which looks unchanged, so the question is: how change of JVM could affect sed? And how can i fix it?
So i found an answer to this question: My guess was wrong, and everything with sed is fine. Problem however was in how Oracle JVM works with external libraries compare to OpenJDK. It did throw exception where script was not expecting it, and it ruin whole sed input.
You can fix it by adding following system variables:
HADOOP_COMMON_LIB_NATIVE_DIR which should point to /lib/native folder of your Hadoop installation and add -Djava.library.path=/opt/hadoop/lib to whatever options you already have in HADOOP_OPTS variable (notice that /opt/hadoop is my installation folder, you might need to change it in order for stuff to work properly).
I personally add export commands to hadoop-env.sh script, but adding it to .bash file or start-all.sh should work as well.

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'

Connecting to remote Oracle server through shell script

i am new to shell script. i have a Oracle server which is remote say "Oracle1" and listening to port 1521 i am trying to remotely connect to it through shell script. i am facing some issue in setting environment variables. my script is.
ORACLE_SID=wctest98;
export ORACLE_SID
ORACLE_HOME=Oracle1.com:1521/opt/oracle/oracle11g/product/11.2.0;
export ORACLE_HOME
PATH=$ORACLE_HOME/bin:$PATH;
export PATH
SQLPATH=$ORACLE_HOME/sqlplus/admin;
export SQLPATH
sqlplus -s /NOLOG << EOF
connect wcadmin/wcadmin#Oracle1.com:1521/wctest98
select * from dual;
exit
EOF
i am getting "sqlplus: not found" error. i am doing it for the first time. i have connected to local Oracle System but not to remote server. feel free to edit...
Your ORACLE_HOME (below) looks incorrect to me. It should not contain the host and port of your oracle server.
ORACLE_HOME=Oracle1.com:1521/opt/oracle/oracle11g/product/11.2.0/bin;
ORACLE_HOME is the path to wherever you have installed the Oracle client on the local machine. For example, it might be:
ORACLE_HOME=/opt/oracle/oracle11g/product/11.2.0
Check if you have the sqplus executable in ${ORACLE_HOME}/bin.
It seems like sqlplus is not on your PATH variable.
You can see if it is with
which sqlplus
And find where it is with
locate sqlplus
To add something to your path
export PATH=$PATH:/path/to/sqlplusdir/

Resources