The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Execute db2 commands in unix rollthecoin AIX 3 04-25-2008 10:17 PM
lom don't execute commands pasalagua SUN Solaris 6 01-25-2008 01:22 PM
Can Xargs execute multiple commands of evry input file nilesrex Shell Programming and Scripting 4 08-30-2006 05:39 AM
how do i get my script to execute multiple commands? hvincent Shell Programming and Scripting 1 04-26-2006 05:19 AM
Execute commands parallel in a for loop ? networkfre@k Shell Programming and Scripting 3 11-27-2005 04:26 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-03-2008
Registered User
 

Join Date: Oct 2006
Posts: 14
How to execute multiple commands via ssh

I'm having some problem when I'm trying to remote ssh to another server to execute a sql query statement. The main problem lies with the single quotes, it doesn't recognise the single quote in the query.

To my understand, it's the single quote in the sql query that trigger the problem, tried using backslashes but it doesn't work.

Any advise will be greatly appreciated.

Non working example

# /usr/local/bin/ssh -qn [hostname] 'ORACLE_SID=[SID];export ORACLE_SID;ORACLE_HOME=`/usr/local/bin/dbhome lss`;export ORACLE_HOME;$ORACLE_HOME/bin/sqlplus -s [username]/[password]<< EndOfFile
> select unique granted_role from dba_role_privs where GRANTED_ROLE='DBA';
> exit;
> EndOfFile'
select unique granted_role from dba_role_privs where GRANTED_ROLE=DBA
*
ERROR at line 1:
ORA-00904: "DBA": invalid identifier


Working example

# /usr/local/bin/ssh -qn [hostname] 'ORACLE_SID=[SID];export ORACLE_SID;ORACLE_HOME=`/usr/local/bin/dbhome lss`;export ORACLE_HOME;$ORACLE_HOME/bin/sqlplus -s [username]/[password]<< EndOfFile
> select * from global_name;
> exit;
> EndOfFile'


Thanks And Regards
Eugene
Reply With Quote
Forum Sponsor
  #2  
Old 01-03-2008
wempy's Avatar
Registered User
 

Join Date: Jun 2006
Location: Harpenden, UK
Posts: 177
Quote:
Originally Posted by srage View Post

Non working example

# /usr/local/bin/ssh -qn [hostname] 'ORACLE_SID=[SID];export ORACLE_SID;ORACLE_HOME=`/usr/local/bin/dbhome lss`;export ORACLE_HOME;$ORACLE_HOME/bin/sqlplus -s [username]/[password]<< EndOfFile
> select unique granted_role from dba_role_privs where GRANTED_ROLE='DBA';
> exit;
> EndOfFile'
select unique granted_role from dba_role_privs where GRANTED_ROLE=DBA
*
ERROR at line 1:
ORA-00904: "DBA": invalid identifier
have you tried using double quotes around the name ("DBA") rather than single quotes.
Reply With Quote
  #3  
Old 01-03-2008
Registered User
 

Join Date: Oct 2006
Posts: 14
Quote:
Originally Posted by wempy View Post
have you tried using double quotes around the name ("DBA") rather than single quotes.
I tried before too

# /usr/local/bin/ssh -qn [hostname] 'ORACLE_SID=[SID];export ORACLE_SID;ORACLE_HOME=`/usr/local/bin/dbhome lss`;export ORACLE_HOME;$ORACLE_HOME/bin/sqlplus -s [username]/[password]<< EndOfFile
> select unique granted_role from dba_role_privs where GRANTED_ROLE="DBA";
> exit;
> EndOfFile'

select unique granted_role from dba_role_privs where GRANTED_ROLE="DBA"
*
ERROR at line 1:
ORA-00904: "DBA": invalid identifier

It gives the same error, but for the sql statement to be valid, I need the single quote.

Using backslash >> terminated even before I terminate it.

# /usr/local/bin/ssh -qn [hostname] 'ORACLE_SID=[SID];export ORACLE_SID;ORACLE_HOME=`/usr/local/bin/dbhome lss`;export ORACLE_HOME;$ORACLE_HOME/bin/sqlplus -s [username]/[password]<< EndOfFile
> select unique granted_role from dba_role_privs where GRANTED_ROLE=\'DBA\';
#
Reply With Quote
  #4  
Old 01-03-2008
rubin's Avatar
Registered User
 

Join Date: Nov 2007
Posts: 216
The problem lies in setting right the environment in a different machine using ssh. In your query there are many special characters that need to be escaped, not only single quotes, but backquotes, $ signs, etc....
Example, from server A:
Code:
ssh user@serverB "hostname; echo `pwd`"           # same thing here for echo $HOME

Output ---> name_serverB, /home/dir_serverA

and not /home/dir_serverB.
You need to go:
Code:
ssh user@serverB "hostname; echo \`pwd\`"
But to globally escape all the special characters, use this code:

Code:
ssh user@serverB <<\EOF
your sql query....
.....
.....
EOF
or

Code:
ssh user@serverB <<'EOF'
your sql query....
.....
.....
EOF

Good luck

Last edited by rubin; 08-01-2008 at 10:14 PM. Reason: added additional code
Reply With Quote
  #5  
Old 01-03-2008
Registered User
 

Join Date: Oct 2006
Posts: 14
But the scenario works with the a query with no condition (where condition)

eg.

Working SQL query
select * from global_name;

Non-Working SQL query
select unique granted_role from dba_role_privs where GRANTED_ROLE='DBA';

Somehow I need to escape the single quote in the select statement if it's possible because of the single quotes in front which is needed to execute multiple commands after I ssh over to the remote host.
Reply With Quote
  #6  
Old 01-04-2008
rubin's Avatar
Registered User
 

Join Date: Nov 2007
Posts: 216
Did you try my last suggestion ? I have no problem running commands in a ssh session with the below code that have single quotes in them:
Code:
ssh user@serverB <<\EOF
your sql query....
.....
.....
EOF
You have another HERE document in your script that you might want to take in consideration.

Quote:
...because of the single quotes in front which is needed to execute multiple commands after I ssh over to the remote host.
Not entirely true, you can use double quotes , and a HERE document too in a ssh session:

Quote:
ssh user@serverB "command1; command2;...."
Quote:
ssh user@serverB <<EOF
command1
command2
....
EOF
Executing multiple commands remotely is problematic. Take a look at this thread:

IF condition failing in a SSH script
Reply With Quote
  #7  
Old 01-04-2008
Registered User
 

Join Date: Oct 2006
Posts: 14
I need to set the environment variables like ORACLE_HOME, SID without playing around with the user profile, that's why the need to execute multiple commands in the ssh command.

The below work fines given it only execute sql queries. But I still need to set the environment settings.

Code:
ssh user@serverB <<\EOF
your sql query....
.....
.....
EOF
Will try again, thanks alot
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 07:31 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0